我的HTML看起来像这样
<select ng-options="possibleConflict.id as possibleConflict.value for possibleConflict in possibleConflicts" ng-model="key"></select>
,控制器看起来像这样
app.controller('MainCtrl', function($scope) {
$scope.key = 2; // Note This value can change
$scope.possibleConflicts = [{
id: '1',
value: 'red'
}, {
id: '2',
value: 'blue'
}]
}
Here is a working Plunker example
为什么没有&#34;红色&#34;页面加载时选择为默认值?如何将其设置为默认值?
答案 0 :(得分:1)
设置$scope.key = "1";
它应该可以解决问题。它必须是一个字符串值,因为这是你在数组中定义的。
答案 1 :(得分:0)
尝试将key
变量更改为:
$scope.key = $scope.possibleConflicts[1].id;
或更改模板和key
变量:
<select ng-options="possibleConflict as possibleConflict.value for possibleConflict in possibleConflicts" ng-model="key"></select>
与
一起$scope.key = $scope.possibleConflicts[1];
答案 2 :(得分:0)
您应将默认值设置为字符串:'2'
$scope.def = '2';
并选择了“蓝色”。
答案 3 :(得分:0)
工作演示:
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope) {
$scope.possibleConflicts = [{
id: '1',
value: 'red'
}, {
id: '2',
value: 'blue'
}];
$scope.key = $scope.possibleConflicts[0].id; // Note This value can change
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<select ng-options="possibleConflict.id as possibleConflict.value for possibleConflict in possibleConflicts" ng-model="key"></select>
</div>
&#13;