javascriptangularjsng-submit

Submit array data with ng-submit get error Cannot read property 'name' of undefined


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?


Solution

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

    Example

     $scope.formData = {};