4

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:

http://plnkr.co/edit/ddiH78pzqE3fsSoq8gAr?p=preview

1 Answer 1

8

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>

Here's the plunk

Sign up to request clarification or add additional context in comments.

3 Comments

Another alternative: <button type="button" class="btn" ng-model="checkboxModel[company.id]" btn-checkbox>{{company.name}}</button> although @rGil approach makes more sense for me.
Awesome, 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!
Good. 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. Then ng-model=checkboxModel.truthy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.