我要遍历一个范围并为每个索引填充新的选择选项。这些选项与我要遍历的范围无关,而是不同类型的选项。代码如下:
<div ng-repeat="i in range(booking.numberOfRooms) track by $index">
<p>Room {{$index + 1}}</p>
<select ng-model="booking.roomSelection[$index]" ng-options="obj.roomType as obj.roomType for obj in roomTypes" ng-init="booking.roomSelection[$index] = { id: $index + 1, roomType: 'Double' }"> </select>
</div>
如何将对象数组分配给ng-model(就像ng-init中一样)?例如。对于两个房间,ng-model的结果应类似于:
booking.roomSelection = [{id: 1, roomSelection: 'Double'}, {id: 2, roomSelection: 'Double'}]
最佳答案
只需绑定(bind)到booking.roomSelection
并删除ng-init
。如果您选择两个选项,则它们的值将pushed
放入booking.roomSelection
中,而booking.roomSelection
将是一个数组
<select multiple ng-model="booking.roomSelection" ng-options="obj.roomType as obj.roomType for obj in roomTypes"></select>