答案 0 :(得分:1)
检查以下plunkr ..
https://plnkr.co/edit/CLue567fPpxp5gXMobcP?p=preview
<select class="form-control" name="document_type" ng-model="document_type"
ng-options="opt as opt.label for opt in options" ng-change="func()" required></select>
$scope.func=function(){
alert($scope.document_type.value);
}
答案 1 :(得分:1)
试试以下
<select class="form-control" ng-model="document_type"
ng-options="opt as opt.label for opt in options">
</select>
<div>{{ document_type }}</div>
$scope.options = [{
label: '',
value: ''
}, {
label: "ID card",
value: "ID card"
}, {
label: 'Passport',
value: 'Passport'
}, {
label: 'Driving license',
value: 'Driving license'
}];
$scope.document_type = $scope.options[2];
在您的问题中,您将文本设置为所选选项,但在实际实现中,您将JSON格式的数据添加为ng-option。如果您设置了正确的格式数据,它将被选中。
答案 2 :(得分:1)
您的选择实际上是将document_type
设置为具有label
和value
属性的对象,但您尝试将默认值指定为字符串。
如果您希望document_type
是字符串而不是对象,则应稍微更改ng-options
子句。而不是opt as opt.label ...
使用opt.value as opt.label ...
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.options = [{
label: '',
value: ''
}, {
label: "ID card",
value: "ID card"
}, {
label: 'Passport',
value: 'Passport'
}, {
label: 'Driving license',
value: 'Driving license'
}];
$scope.document_type="Passport";
});
&#13;
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
Document type
<select class="form-control" name="document_type" ng-model="document_type"
ng-options="opt.value as opt.label for opt in options" required></select>
</body>
</html>
&#13;
答案 3 :(得分:0)
根据您进行此分配的上下文,您可能需要在其后面包含以下调用:
$scope.$apply() ;
试试这个。