16

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!

Blackle Mori
  • 1,131
  • 1
  • 8
  • 14

1 Answers1

25

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
kryger
  • 12,906
  • 8
  • 44
  • 65
pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286