AngularJS Bootstrap UI btn-checkbox in ng-repeat
The answer you linked is the same solution for this problem. Each button within the repeat needs it's own unique property of the model. If they are all set to the same model, as in the plunk $scope.checkboxModel = {id: 0}
, when one button is checked, they will all be checked.
So to give each button uniqueness, you could set another property onto the objects within the ng-repeat
. This property would hold a boolean value that changes on check. So your model would look like:
$scope.companies = [{"id":2,"name":"A", truthy:false}] // and so on
You would not need to explicitly set this in the controller - just declare the new property right in the button element's model:
<companies ng-repeat="company in companies">
<button type="button" class="btn" ng-model="company.truthy"
btn-checkbox>{{company.name}}</button>
</companies>
Related videos on Youtube
Brian
Working at JAMF software in the development operations group.
Updated on June 04, 2022Comments
-
Brian about 2 years
I have having some trouble utilizing the angular bootstrap-ui btn-checkbox directive and its interaction with a ng-repeat directive. The way the directive appears to be setup is that you have to manually name each individual model for a multi checkbox scenario, which is either not possible within an ng-repeat or I haven't found how to accomplish this.
I found an answer somewhat similar to this issue:
Setting and getting bootstrap radio button inside angular repeat loop
and forked the plunker to better explain exactly what I am seeing as a problem. The plunker can be viewed:
-
pkozlowski.opensource almost 11 yearsAnother alternative:
<button type="button" class="btn" ng-model="checkboxModel[company.id]" btn-checkbox>{{company.name}}</button>
although @rGil approach makes more sense for me. -
Brian almost 11 yearsAwesome, my issue was I was trying to create a new model not modify the existing one within the ng-repeat scope which is why I was having a hard time getting my head around this one. Thanks!
-
rGil almost 11 yearsGood. You could also create a new model, as you had originally. Just make sure it is an array of the same length as the ng-repeat, and is populated by
{}
objects. Like:$scope.checkboxModel = [{},{}]
. and so on. Thenng-model=checkboxModel.truthy
.