[英]AngularJs: Radio Button Not Selected By Default
I have the following snippet in my directive template 我的指令模板中包含以下代码段
'<li ng-repeat="f in foos">' +
'<input type="radio" ng-change="foo(f.key)" ng-model="selectedFoo" name="foos" id="{{f.key}}" value="{{f.key}}">{{f.value}}</input>' +
'</li>' +
In my link method I have 在我的链接方法中
scope.foos = [
{ key: 'a', value: 'A', checked: true, symbol: 'a' },
{ key: 'b', value: 'B', symbol: 'b' },
{ key: 'c', value: 'C', symbol: 'c' }
];
scope.selectedFoo = "a";
I have method foo that does this 我有这样做的方法foo
scope.foo = function(selectedValue) {
scope.selectedMatchType = selectedValue;
};
There are two problems that I am facing 我面临两个问题
What is wrong here? 怎么了
Please note that ng-repeat creates it's own scope for every template which means you'll have to use $parent in ng-model for the input. 请注意, ng-repeat会为每个模板创建自己的作用域,这意味着您必须在ng-model中使用$ parent进行输入。
ng-model="$parent.selectedFoo"
Also a working fiddle with your code example: http://jsfiddle.net/hpeinar/5gj9y6k4/ 还可与您的代码示例配合使用: http : //jsfiddle.net/hpeinar/5gj9y6k4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.