2016-05-06 50 views
3
<span ng-repeat="s in colors"> 
     <p><li><input type="checkbox" ng-model="colors[id].checked" ng-change="setColor(s.id)"> &nbsp; {{s.name}}</li></p> 
</span> 

在我的控制器NG-重複循環,上覆選框

$scope.colorsel= []; 
    $scope.setColor = function(a){ 
     if($scope.colorsel.indexOf(a) == -1){ 
       $scope.colorsel.push(a); 
     } 
     else{ 
      var index = $scope.colorsel.indexOf(a) 
      $scope.colorsel.splice(index,1); 

     } 
    } 

上午正確獲取colorsel值。但是,如果我選擇一個複選框,它只是顯示所有複選框被選中(表示所有複選框中出現刻度標記,但它僅取自在色彩數組中選中的複選框的ID)。如果我選擇另一個,所有刻度線都將消失。 Plz幫助我解決這個問題。

回答

1

只是直接引用對象:ng-model="s.checked"

這個:colors[id].checked,因爲你引用了相同ID(也許s.idcolors[s.id].checked是你想要什麼)是錯誤的。因此,如果您的控制器$scope.id = 2中的數據總是比參考colors[2].checked

+0

thanku ................ ur ans works ... – athi

0

嘗試,如果你想從你需要使用的顏色「S」就地色彩訪問值這個

<span ng-repeat="s in colors"> 
<p> 
    <input type="checkbox" ng-model="s[id].checked" ng-change="setColor(s.id)"> &nbsp; {{s.name}} 
</p> 
</span> 
+0

thanku ... :) Ur ans works .. – athi

0

ng-repeat="s in colors"foreach(s in colors) 如此相似。

+0

thanku ... :) Ur ans works .. – athi