我迭代一个范围并为每个索引填充新的选择选项。这些选项与我迭代的范围无关,但是是不同类型的选项。代码如下:
<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'}]
答案 0 :(得分:1)
只需绑定到您的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>
答案 1 :(得分:1)
原来必须将ng-model属性更改为:booking.roomSelection[$index].roomType
对象数组也必须在控制器中声明,例如$scope.booking.roomSelection = [];
完整的声明是:
<div ng-repeat="i in range(booking.numberOfRooms) track by $index">
<p>Room {{$index + 1}}</p>
<select ng-model="booking.roomSelection[$index].roomType" ng-options="obj.roomType as obj.roomType for obj in roomTypes" ng-init="booking.roomSelection[$index] = { id: $index + 1, roomType: 'Double' }"></select>
</div>