2014-06-15 12 views
0

나는 서버에 전달해야하는 입력 숨김 필드가있는 ng-repeat를 포함하는 양식을 가지고 있습니다.AngularJS에서 객체 배열에 필드 추가

<form ng-controller="FrmController" method="post"> 
      <div class="orderlist">      
        <table class="table gas"> 
         <thead> 
          <tr> 
          <th scope="col" ng-click="orderByField='image'; reverseSort = !reverseSort">Immagine 

          <span ng-show="orderByField == 'image'" class="listorder"> 
          <span ng-show="!reverseSort"><i class="fa fa-sort-desc"></i>   </span> 
          <span ng-show="reverseSort"><i class="fa fa-sort-asc"></i></span> 
             </span> 
             </th> 

     [...] 

         </thead> 
         <tbody id="gasbody" ng-repeat="product in products | filter: searchQuery | orderBy:orderByField:reverseSort" class="repeated-item"> 
         <tr> 
          <input type="hidden" name="form-x-id" ng-model="form.form-x-id"> 

(X는 NG 반복의 인덱스를 나타낸다)이 제어기의 코드

[...] 

<button ng-click="addtoCart();" class="def-btn" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</button> 

</form> 

이다 :

function FrmController($scope,$http,transformRequestAsFormPost){ 
       $scope.form = {}; 
       $scope.addtoCart = function() { 
        $http({ 
         method: 'POST', 
         url: '', 
         data: $.param($scope.form), 
         headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} 
        }); 
       } 
} 

제가 이 페이지의 코드 다음 형식으로 데이터를 보내야합니다.

form-1-id: value 
form-2-id: value2 
form-3-id: value3 

$scope.form = {form-1-id: value, 
       form-2-id: value2, 
       form-3-id: value3, ... 
       ... form-n-id: valuen} 

여기서 n은 ng-repeat의 길이를 나타냅니다.

ECC ....

어떻게 동적으로 NG-모델 바인더 제본 달러 (A $)의 범위에 필드를 추가 할 수 있습니까? 가능합니까?

감사합니다.

답변

0

정확하지는 않습니다. 원하는 것은 무엇입니까? ng-repeat의 색인을 원하십니까?

당신은이

 <input name="form-{{$index}}-id" type="Hidden" ng-model="form[$index]"> 

같은 $ 인덱스를 사용하거나 동적으로 폼에 뭔가를 밀어 하시겠습니까? 당신은 사용할 수 있습니다 : 물론

 $scope.addToForm=function(newItem){ 
     $scope.form.push(newItem) 

     } 

당신은 두 번째 솔루션은 최적의 ($ scope.form.push (인 newItem))이었다 []

+0

= 형성, 양식 =을 {} 변경해야합니다. 감사합니다;) – Dyd666

+0

환영합니다. 도와 줘서 기뻐요. – Milad