Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI.Bootstrap's radio buttons act strange with ng-repeat [duplicate]

I have a problem dynamically generating options for a radio model in angular's ui.bootstrap. I thought I could simply ng-repeat over an array, using it's contents for the btn-radio attribute like so:

//in the controller
$scope.radioModel =  undefined;
$scope.radioModelButtons = ["a", "b", "c"];

//in the html
<div class="btn-group" >
  <button ng-repeat="value in radioModelButtons"
    class="btn" type="button" ng-model="radioModel"
    btn-radio="'{{value}}'">
      {{value}}
  </button>
</div>

I'm using angular 1.1.4 and ui.bootstrap 0.3.0.

Here is a jsfiddle of my efforts, as you can see, the radio buttons act independently and do not affect the radioModel variable.

Thanks!

like image 335
Blackle Mori Avatar asked May 08 '13 14:05

Blackle Mori


1 Answers

This is how you should write your markup:

<button ng-repeat="value in radioModelButtons"
        class="btn" type="button" ng-model="radio.model"
        btn-radio="value">
          {{value}}
</button>

And the working jsFiddle: http://jsfiddle.net/yMLqz/2/

There were 2 problems in your approach:

  • btn-radio should be used with AngularJS expression, and not an interpolated value
  • ng-repeat is creating a new scope so you need to take this into account if you want to bind to a value defined on a parent scope
like image 113
pkozlowski.opensource Avatar answered Oct 11 '22 13:10

pkozlowski.opensource