例如,我有一个带有名为 'houses'
的数组属性的 Controller ,并且我在带有一组单选按钮的表格行上ng-repeat
-ing (真/假,或者你有什么)。这些单选按钮上的模型应该是什么,以便选择其中任何一个按钮都会设置模型数组中相应项目的值?我尝试将模型设置为:
ng-model="array[{{$index}}]"
但这没有用。我取出了 {{ }}
,只留下了文字字符串“$index”。
这在 Angular 中可能吗?
最佳答案
它应该可以在没有大括号的情况下工作。我做了一个Plunker .
$scope.houses = [true, false, true, false, false];
<ul>
<li ng-repeat="house in houses">
Checked: <input type="checkbox" ng-model="houses[$index]" />
</li>
</ul>
{{houses}}
我不明白您是否有一个复杂的对象并想要设置其属性(即 ng-model="houses[$index].checked"
。如果是这种情况,那么您应该只使用迭代器(即 ng-model="house.checked"
),因为 house
将迭代每个数组对象。
关于javascript - 如何将一组重复的单选按钮映射到 Angular Controller 模型上的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732934/