2016-02-23 135 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); 
    } 
}