[英]Data-binding to ng-model inside of checkbox
My problem is that I'm trying to access these checkboxes from inside the controller so I can set them to true from there. 我的问题是我试图从控制器内部访问这些复选框,因此可以从那里将它们设置为true。
For some reason, I can't figure out the correct ng-model to use for these checkboxes such that I have the ability to do so, while maintaining that each one has a unique name. 由于某种原因,我无法找出用于这些复选框的正确ng模型,因此我有能力做到这一点,同时又要确保每个人都有唯一的名称。
<accordion-group ng-repeat="group in groups">
<accordion-heading>{{group.title}}</accordion-heading>
<div ng-repeat="collection in group.collections">
<div class="checkbox collection-legend" style="background-color: {{collection.color}}">
<label><input type="checkbox" ng-click="toggleCollection(collection.key)" ng-model="checkboxes[collection.key]"> {{collection.name}}: <em>{{collection.description}}</em></label>
<div class="customFields">
<label ng-repeat="(key, val) in collection.queries" ng-if="key != 'default' && key != 'dateRangeKey'">
<input class="form-text" ng-model="customProperties[collection.name + '-' + key]" type="text"><em>{{key}}</em></label>
</div>
</div>
</div>
</accordion-group>
For some context, I'm trying to set up a "restore session" method, where the user can bring back up old forms submitted without having to fill them out again, so I have the list of checkboxes that need to be "rechecked", I just can't figure out how to name my model so I can do it. 在某些情况下,我试图建立一种“恢复会话”方法,用户可以在不重新填写表单的情况下恢复提交的旧表单,因此我有需要“重新选中”的复选框列表,我只是想不出如何命名模型,所以我可以做到。
ng-model="checkboxes[collection.key]" seems like it should work here. ng-model =“ checkboxes [collection.key]”似乎应该在这里工作。 Any advice?
有什么建议吗?
And then $scope.checkboxes[key] = true in the controller. 然后$ scope.checkboxes [key] = true在控制器中。 (key matches collection.key; I've already confirmed this).
(密钥与collection.key匹配;我已经确认了这一点)。 Getting a "can not set (key) of undefined" error.
出现“无法设置(键)的未定义”错误。
Here is the JS: 这是JS:
$scope.session.collections.forEach(function(c){
$scope.toggleCollection(c);
console.log(c + ' '+ typeof c);
$scope.checkboxes = {
c: true
};
});
Solution: Never initialized $scope.checkboxes as an empty object. 解决方案:切勿将$ scope.checkboxes初始化为空对象。 Works now.
现在可以使用。
The code you pasted certainly works, I've verified it here: https://jsfiddle.net/ho8art1t/ 您粘贴的代码肯定可以正常工作,我已经在这里进行了验证: https : //jsfiddle.net/ho8art1t/
checkboxes should be something along those lines: 复选框应遵循以下原则:
$scope.checkboxes = {
a: true,
b: false,
c: true
};
You should either paste the JS that goes with it or just check that your objects are placed similarly as in that fiddle. 您应该粘贴随附的JS,或者只是检查对象的放置方式与小提琴中的放置方式类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.