admin管理员组文章数量:1332382
I have a group of material design checkboxes and I would like to bind their values to an array in my controller.
To acplish this I used the first method described in this SO answer. While the values are properly being added and removed from the list, the boxes no longer display as "checked" when I click on them. My code is below and I also recreated the problem in this codepen.
HTML for my checkbox
<md-checkbox
ng-repeat="site in websites"
value="{{site}}"
ng-checked="selection.indexOf(site) > -1"
ng-click="toggleSelection(site)">
{{site}}
</md-checkbox>
JavaScript from Controller
$scope.websites = ['Facebook', 'Twitter', 'Amazon'];
$scope.selection = ['Facebook'];
$scope.toggleSelection = function toggleSelection(site) {
var idx = $scope.selection.indexOf(site);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(site);
}
};
});
I have a group of material design checkboxes and I would like to bind their values to an array in my controller.
To acplish this I used the first method described in this SO answer. While the values are properly being added and removed from the list, the boxes no longer display as "checked" when I click on them. My code is below and I also recreated the problem in this codepen.
HTML for my checkbox
<md-checkbox
ng-repeat="site in websites"
value="{{site}}"
ng-checked="selection.indexOf(site) > -1"
ng-click="toggleSelection(site)">
{{site}}
</md-checkbox>
JavaScript from Controller
$scope.websites = ['Facebook', 'Twitter', 'Amazon'];
$scope.selection = ['Facebook'];
$scope.toggleSelection = function toggleSelection(site) {
var idx = $scope.selection.indexOf(site);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(site);
}
};
});
Share
edited May 23, 2017 at 10:28
CommunityBot
11 silver badge
asked Apr 5, 2015 at 2:57
dsal1951dsal1951
1,7603 gold badges18 silver badges21 bronze badges
1
- Since the v 0.9 release of Angular Material it looks like the code above now works, while @jarz code works for v0.8 and below – dsal1951 Commented May 22, 2015 at 16:42
1 Answer
Reset to default 4Try changing this:
ng-checked="selection.indexOf(site) > -1"
to this:
ng-checked="{{selection.indexOf(site) > -1}}"
Worked for me: http://codepen.io/anon/pen/xbNOmE
本文标签: javascriptBinding Angular Material Design Checkboxes to an Array in ControllerStack Overflow
版权声明:本文标题:javascript - Binding Angular Material Design Checkboxes to an Array in Controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293966a2448347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论