Setting and getting bootstrap radio button inside angular repeat loop

前端 未结 1 1188
悲&欢浪女
悲&欢浪女 2021-01-03 23:47

I am trying to set the default button and get the current selected value. The example without the repeat loop works.

Here is my plnkr: http://plnkr.co/edit/t9CefA5bh

1条回答
  •  旧时难觅i
    2021-01-04 00:26

    So, there were 2 things going on in your plunker.

    Firstly the btn-radio attribute doesn't need interpolation ({{}}) you can (and should) provide an expression directly. So just write btn-radio="company.id" instead of btn-radio="{{company.id}}".

    Secondly, you must know that the ng-repeat directive creates a new scope. This is a very, very common conceptual issue people have with AngularJS so I would encourage you to read https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance

    Coming back to you particular problem you can either change you ng-model expression to point to a parent scope (ng-model="$parent.radioModel") or bind to an object property (ng-model="radioModel.id").

    Here is a working plunk with the second approach: http://plnkr.co/edit/uGAxaRlPFK6sD4tRjGXX?p=preview

    0 讨论(0)
提交回复
热议问题