twitter-bootstrap 在角度重复循环中设置和获取引导单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16265993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:00:51  来源:igfitidea点击:

Setting and getting bootstrap radio button inside angular repeat loop

javascripthtmltwitter-bootstrapangularjs

提问by Genady Okrain

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/t9CefA5bhLZs3RASmEUG?p=preview

这是我的 plnkr:http://plnkr.co/edit/t9CefA5bhLZs3RASmEUG?p=preview

It is based on this example: http://plnkr.co/edit/LFj4inY9TLYZs9z7yHCr?p=preview

它基于此示例:http: //plnkr.co/edit/LFj4inY9TLYZs9z7yHCr?p=preview

Thank you!

谢谢!

回答by pkozlowski.opensource

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

因此,您的 plunker 中发生了两件事。

Firstly the btn-radioattribute 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}}".

首先,该btn-radio属性不需要插值 ( {{}}),您可以(并且应该)直接提供表达式。所以只写btn-radio="company.id"而不是btn-radio="{{company.id}}".

Secondly, you must know that the ng-repeatdirective 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

其次,您必须知道该ng-repeat指令创建了一个新的作用域。这是人们对 AngularJS 的一个非常非常普遍的概念问题,所以我鼓励你阅读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").

回到您的特定问题,您可以将 ng-model 表达式更改为指向父作用域 ( ng-model="$parent.radioModel") 或绑定到对象属性 ( ng-model="radioModel.id")。

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

这是第二种方法的工作原理:http://plnkr.co/edit/uGAxaRlPFK6sD4tRjGXX? p=preview