Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Bootstrap UI btn-checkbox in ng-repeat

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

like image 732
Brian Avatar asked Feb 16 '23 23:02

Brian


1 Answers

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

like image 137
rGil Avatar answered Feb 18 '23 12:02

rGil