2017-10-17 21 views
0

我有一個包含對象的數組。每個對象都有一個布爾變量(array [i] .check)。我想要使​​用AngluarJS通過點擊複選框直接我array.I內更改特定對象的布爾值的表使用:陣列中對象的角度複選框

<table> 
    <tr data-ng-repeat="a in array"> 
      <td style="width:200px;"> {{a.name}}</td> 
      <td><input type="checkbox" ng-model="a.check" ng-checked="a.check"></td> 
    </tr> 
</table> 

但是,每一個布爾值仍然假始終。我做錯了什麼?

+1

是價值'a.check'布爾或者一個字符串?換句話說,如果你看看實際的值是「假」還是「假」? – zero298

+1

此外,你不應該混合'ngModel'和'ngChecked'。查看文檔:[ngChecked](https://docs.angularjs.org/api/ng/directive/ngChecked)。 – zero298

+0

a.check是布爾值,如從布爾值 – AlexP

回答

1

你可以試試這種單獨使用ng-model的方法,我已經創建了一個工作方法的小提琴,應用這個代碼,然後與問題分享回來,我會爲你解決它!

var app = angular.module('myApp', []); 
 

 
app.controller('MyController', function MyController($scope) { 
 
\t $scope.array = [{id:1, name: "test1", changeThisValue: false},{id:2, name: "test2", changeThisValue: true},{id:3, name: "test3", changeThisValue: true},{id:4, name: "test4", changeThisValue: true},{id:5, name: "test5", changeThisValue: true}]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-controller='MyController' ng-app="myApp"> 
 
    <table> 
 
    <tr data-ng-repeat="a in array"> 
 
     <td style="width:200px;"> {{a.name}}</td> 
 
     <td> 
 
     <input type="checkbox" ng-model="a.changeThisValue"> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
    <pre style="width:100px;white-space: pre-wrap;">{{array}}</pre> 
 
</div>

+0

只使用'ngModel'工作!我第一次嘗試我犯了一個錯誤,沒有注意到..... – AlexP

+0

@AlexP很高興你解決它! –