【发布时间】:2015-06-14 21:08:28
【问题描述】:
我一直希望使用 ng-repeat 从屏幕上显示的单选按钮列表中检查一个单选按钮,但我的代码不起作用。这就是我正在做的事情:
<div class="clubRole" data-ng-if="club.checked">
<div data-ng-repeat="role in securityGroups.slice(0,1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode" checked="checked"> {{role.description}}
</div>
<div data-ng-repeat="role in securityGroups.slice(1,securityGroups.length+1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode"> {{role.description}}
</div>
</div>
代码的目的是让第一个单选按钮被选中,而其他的不被选中。该代码有一个问题:它不起作用。但至少它给出了我想要做的事情的想法:我希望默认选中其中一个单选按钮,无论它是哪一个。
【问题讨论】:
-
你应该使用
data-ng-model="$parent.club.role",因为你使用了ng-if -
@pankajparkar,我相信是
ng-repeat创建了自己的范围,而不是 ng-if -
@Victor
ng-if也新建了一个作用域,需要看docs.angularjs.org/api/ng/directive/ngIf
标签: angularjs html radio-button