1

I want to submit a form with array data

<form ng-submit="processForm()">
     <div class="item item-text-wrap item-toggle" ng-repeat="item in items | orderBy: ['id','name']">
         {{item.name}}
         <label class="toggle toggle-calm">
              <input type="checkbox" ng-model="formData[$index].id" ng-true-value="{{item.id}}" />
             <div class="track">
                <div class="handle"></div>
             </div>
         </label>
     </div>
     <div class="item">
        <button class="button button-block button-calm">Submit</button>                
     </div>
</form>

In controller:

.controller('ProcessCtrl', function ($scope, $http, $localStorage, $state) {
        $scope.formData = [];
        $scope.processForm = function () {
            $http({
                method: 'post',
                url: 'process.php',
                data: $.param($scope.formData),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .success(function (result) {
                 console.log(result);
            })
        }
    })

When I submit it I get this error Cannot read property 'name' of undefined. Can anyone point me what is wrong with the code?

Abaij
  • 853
  • 2
  • 18
  • 34

1 Answers1

1

Did you try making your $scope.formData an object ?

Example

 $scope.formData = {};
code.cycling
  • 1,246
  • 2
  • 15
  • 33
  • ah, it's working. however, I don't quite understand why use `{}` instead of `[]` considering the data submitted are in array – Abaij Nov 22 '17 at 02:48
  • 1
    @Abaij its because your passing your value to an object example `formData[$index].id` you declared `formData` as an object by declaring `.id` – code.cycling Nov 22 '17 at 02:55