问题描述
我创建了一些带有动态模型名称的复选框,如下所示:
I have created a some checkboxes with dynamic model name like this:
<label ng-repeat="item in main.itemDetails">
<input type="checkbox" checklist-value="item.price"
ng-click="itemChanged(item)" ng-model="checkboxes[item.name]">
{{item.name}} - <b>{{item.price}} €</b>
<br>
</label>
在我的控制器中,我想检查一些复选框,如果它们的模型名称存在于数组$ scope.selectedItems中.
In my controller, I want to check some checkboxes if their model's name exists in array $scope.selectedItems.
我正在尝试做这样的事情,但是它不起作用:
I'm trying to do something like this but it isn't working:
for (var i = 0, j = $scope.selectedItems.length; i<j; i++) {
$scope['selectedItems[i].name'].isChecked = true;
}
例如,如果item.name ='Item1',我希望将模型命名为$ scope.Item1,然后再调用$scope.Item1.isChecked = true;
for example if item.name = 'Item1' I want the model to be named $scope.Item1 and later I want to call $scope.Item1.isChecked = true;
我在这里做什么错了?
推荐答案
您正在将模型存储在$scope.checkboxes
中,因此可以像下面的代码一样将其值设置为true
.
Your are storing the model in the $scope.checkboxes
, so you can set its value to true
like the code below.
for (var i = 0, j = $scope.selectedItems.length; i<j; i++) {
$scope.checkboxes[$scope.selectedItems[i].name] = true;
}
这篇关于如何以编程方式选中带有动态模型名称的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!