gpt4 book ai didi

Angularjs 不选择第一个元素

转载 作者:行者123 更新时间:2023-12-03 01:37:44 25 4
gpt4 key购买 nike

我想设置选定的第一个元素。我使用了 ng-init 但不起作用。怎么解决?

HTML

  <select id="room" name="room" ng-init="rooms.id=1" ng-model="searchRoom" class="custom-select">
<option value="{{rooms.id}}" ng-repeat="rooms in searchRoomValues track by $index">{{rooms.value}}</option>
</select>

JS

$scope.searchRoomValues=[{value: 'Not', id:1},{value: '1+0', id:1648}, {value: '1+1', id:1649}, {value: '2+1', id:1650}, {value: '3+1', id:1651}, {value: '4+1', id:1652}, {value: '5+1', id:1653}];        

最佳答案

使用ng-options:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.searchRoomValues = [{
value: 'Not',
id: 1
}, {
value: '1+0',
id: 1648
}, {
value: '1+1',
id: 1649
}, {
value: '2+1',
id: 1650
}, {
value: '3+1',
id: 1651
}, {
value: '4+1',
id: 1652
}, {
value: '5+1',
id: 1653
}];
$scope.selected = $scope.searchRoomValues[0];
});
<body ng-controller="MainCtrl">
<select ng-model="selected" ng-options="item as item.value for item in searchRoomValues"></select>
</body>

Plnkr

关于Angularjs 不选择第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47933515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com