我有一个简单的ng-repeat,带有一个单选按钮,该按钮无法运行。以下工作完全正常:
<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>
但是通过以下操作,我确实看到了两个单选按钮(如预期的那样),并且可以选择两个单选按钮中的一个,但是
$scope.display_as
仍未定义。<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>
为什么哦为什么?!有人能启发我我在
ng-repeat
中做错什么吗? 最佳答案
ng-repeat
为通过$scope
进行的每次迭代创建一个单独的子user.positions
。这些$scope
都不具有display_as
属性。
您可以使用$scope
访问ng-repeat
中的父$parent
。在您的情况下:$parent.display_as
。
关于javascript - 为什么带有ng-repeat的单选按钮不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34580146/