2016-02-23 137 views
0

我有NG-重复一个div:ngRepeat和复选框

<div ng-repeat="fruit in fruits"> 
 
    <input type="checkbox" ng-model="this.value" ng-checked="false">{{fruit.code}} - {{fruit.short_name}} 
 
</div>

这里furits是数组对象和包含域代码,SHORT_NAME。

furits: { 
     { 
      code : code_value1, 
      short_name :short_name_value1 
     }, 
     { 
      code : code_value2, 
      short_name :short_name_value2 
     },... 
     { 
      code : code_value.., 
      short_name :short_name_value.. 
     }, 
     } 

我想在提交按钮单击并在新数组中插入这些代码后,选中复选框的代码。

而且还发送相同的数组到服务器来完成任务。

请帮我解决这个问题。

+0

请在这里添加你的web服务发送数组到服务器 –

回答

0
<div ng-repeat="fruit in fruits"> 
    <input type="checkbox" ng-model="fruit.isSelected" />{{fruit.code}} - {{fruit.short_name}} 
</div> 
<input type="button" ng-click="sendServer()" value="Submit" /> 

而且你的控制器方法都很喜欢,

$scope.sendServer = function(){ 
    $scope.checkedFruits = []; 
    for(i = 0; i < $scope.fruits.length; ++i){ 
    if ($scope.fruits[i].isSelected) 
     $scope.checkedFruits.push(fruit.code); 
    } 
    $scope.postData(); 
} 

//Send array to server via web service with parameter FruitsCode 

$scope.postData = function() { 
    $http.post('http://your.webservice/', {FruitsCode:$scope.checkedFruits}).success(
    function(data){ 
     $scope.response = data 
    }) 

}

希望此举能帮助你!

+0

谢谢你们俩。但是,这一行:“$ scope.fruits [i] .isSelected”返回“未定义”... –

+0

请在视图中更改ng-model =“fruit.isSelected” –

+0

我改变了它,当然... –

0
<div ng-repeat="fruit in fruits"> 
    <input type="checkbox" ng-model="fruit.isChecked" />{{fruit.code}} - {{fruit.short_name}} 
</div> 
<input type="button" ng-click="save()" value="SAVE" /> 

而且你的控制器内:

$scope.save = function(){ 
    var myFruits = []; 
    for(i = 0; i < $scope.fruits.length; ++i){ 
     if ($scope.fruits[i]. isChecked) 
      myFruits.push(fruit.code); 
    } 
}