这是我的 Controller 范围
$scope.all = [
{'ID':'1','NAME':'BOB','BMW':1,'AUDI':'0'},
{'ID':'2','NAME':'PETE','BMW':'0','AUDI':'1'}
];
$scope.cars = [
{'ID':'1','CAR':'BMW'},
{'ID':'2','CAR':'AUDI'}
];
这是我的观点
<table class="table">
<thead>
<tr>
<th>Name</th>
<th ng-repeat="c in cars">{{c.CAR}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="u in all">
<td>{{ u.NAME }}</td>
<td ng-repeat="c in cars">
<input type="checkbox" ng-checked="{{ u.c.CAR }}">
</td>
</tr>
</tbody>
</table>
现在我想根据汽车的值检查复选框 ng-checked="{{ u.c.CAR }}"
但我无法设置它。我在 Firebug 中没有收到任何错误。 AngularJS 是否提供这样的表达式?如果现在,还有什么其他解决方案?我希望将其设置在 View 本身中
更新:我早就应该这么做了。这是我的JSFIDDLE
最佳答案
如果我理解正确的话,您希望将 1
或 0
解析为 true
和 false
。您应该能够执行以下操作:
<tr ng-repeat="u in all">
<td>{{ u.NAME }}</td>
<td ng-repeat="c in cars">
<input type="checkbox" ng-checked="u[c.CAR] == 1">
</td>
</tr>
*注意:您不需要在 ng-checked
内插入 { }
。
这是一个updated fiddle .
关于javascript - 从两个表达式设置 ng-checked 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22811473/