2017-11-22 18 views
1

の「名前」プロパティを読み取ることができませんNG-提出GETエラーで配列データを提出する私は、データアレイコントローラで未定義

<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> 

でフォームを送信したい:

.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); 
      }) 
     } 
    }) 

私はそれを送信するとこのエラーはCannot read property 'name' of undefinedです。コードで間違っている点を誰かが指摘できますか?

答えて

1

$scope.formDataをオブジェクトにしようとしましたか?

$scope.formData = {}; 
+0

ああ、それが働いています。なぜなら、提出されたデータが配列 – Abaij

+1

@Abaijにあると考えると、 '[] 'の代わりに' {} 'を使うのはどういうことなのでしょうか?それはあなたの値をオブジェクトの型に渡すからです。' formData [$ index] .id' '.id'を宣言してオブジェクトとして' formData'を宣言しました。 – Priz