2017-10-17 8 views
0

私はオブジェクトを含む配列を持っています。各オブジェクトはブール値(配列[i] .check)を持っています。私は、チェックボックスをクリックすることで、それが直接私のarray.Iの使用内の特定のオブジェクトのブール値を変更することをAngluarJSを使用してテーブルを作りたい:配列内のオブジェクトの角チェックボックス

<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> 

しかし、すべてのブールの値は常にfalseのまま。私が間違っていることは何ですか?

+1

'a.check'ブールまたは文字列の値はありますか?言い換えれば、実際の値を見ると 'false'か' false' 'ですか? – zero298

+1

さらに、 'ngModel'と' ngChecked'を混在させてはいけません。 docs:[ngChecked](https://docs.angularjs.org/api/ng/directive/ngChecked)を参照してください。 – zero298

+0

a.checkはbooleanで、RESTサーバーからブール値 – 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あなたはそれを固定してうれしい! –

関連する問題