Javascript 为什么带有ng repeat的单选按钮不起作用?
我有一个简单的ng重复,单选按钮不起作用。以下方法非常有效:Javascript 为什么带有ng repeat的单选按钮不起作用?,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个简单的ng重复,单选按钮不起作用。以下方法非常有效: <label class="radio-inline"> <input type="radio" ng-model="display_as" name="display_as" value="1">position 1 <input type="radio" ng-model="display_as" name="display_as" value="2">position 2
<label class="radio-inline">
<input type="radio" ng-model="display_as" name="display_as" value="1">position 1
<input type="radio" ng-model="display_as" name="display_as" value="2">position 2
</label>
<pre>{{ display_as }}</pre>
为什么,为什么?!有谁能告诉我在我的
ng repeat
中做错了什么?ng repeat
通过user.positions
为每次迭代创建一个单独的子$scope
。这些$scope
都没有display\u as
属性
您可以使用
$parent
访问ng repeat
中的父级$scope
。在您的情况下:$parent.display_as
对于某些错误,如果您想使用user.display_as,它可以工作()。Excellent@andrei gheorghiu,过去我也有同样的问题,我只是通过ng change得到的。
<label ng-repeat="position in user.positions" class="radio-inline">
<input type="radio" ng-model="display_as" name="display_as" value="{{ $index }}">{{ position.name }}
</label>
<pre>{{ display_as }}</pre>