2017-05-04 28 views
0

我有一個嵌套的表單....我可以添加新的字段,但在刪除字段時遇到了一些麻煩。如何使用拼接去除angularJS中的嵌套表單

我希望能夠拼接的具體發生在我的對象數組刪除領域

目前,我有...

HTML:

<div ng-repeat="senarioItem in attendees.formData.scenarios[0].scenarioItems" class="row"> 
     <div ng-hide="cost._destroy">  
      <div class="form-group col-xs-5"> 
       <label for="costDescription">Description</label> 
       <input class="form-control input-group-lg" type="text" name="costDescription" ng-model="senarioItem.costDescription"/> 
      </div> 
      <div class="form-group col-xs-2"> 
       <label for="cost">Amount</label> 
       <input 
        class="form-control input-group-lg" 
        type="number" 
        name="cost" 
        ng-model="senarioItem.cost"/> 
      </div> 

      <div class="col-md-2"> 
       <a ng-click="removeCost()" class="btn btn-xs btn-danger">X</a> 
      </div> 
     </div> 
    </div> 

控制器:

attendees.removeCost = function(){ 
      var cost = attendees.formData.scenarios[0].scenarioItems[index]; 
      if(cost.id) { 
       cost._destroy = true; 
      } else { 
       attendees.cost.splice(index, 1); 
      } 


     var cost = attendees.formData.scenarios[0].scenarioItems[index]; 
    }; 

JSON:

"scenarioItems": [ 
     { 
      "cost": "", 
      "costDescription": "", 
     }, 
     { 
      "cost": "", 
      "costDescription": "" 
     }, 
     { 
      "cost": "", 
      "costDescription": "" 
     }, 
     { 
      "cost": "", 
      "costDescription": "" 
     } 
     ] 

回答

0

傳遞索引作爲參數傳遞給功能

<div class="col-md-2"> 
    <a ng-click="removeCost($index)" class="btn btn-xs btn-danger">X</a> 
</div> 

然後在功能中刪除它

attendees.removeCost = function(index){ 
      var cost = attendees.formData.scenarios[0].scenarioItems[index]; 
      if(cost.id) { 
       cost._destroy = true; 
      } else { 
       attendees.cost.splice(index, 1); 
      } 


     var cost = attendees.formData.scenarios[0].scenarioItems[index]; 
};