我正在使用angular的ng選項來填充html的名稱。我希望它預先選擇我設置的ng模型(註冊人已選擇)的值。但由於某種原因,它不會這樣做。ng-options將不會預先選擇一個選項
我查閱了ng-options的各種不同的文檔,看了一堆關於ng-options的其他堆棧溢出帖子,但我似乎無法弄清楚我做錯了什麼。
的代碼可以在this plunker或在下面找到:
的Javascript:
angular.module('app', [])
.controller("MainController", ["$scope", function($scope) {
$scope.paidDuesCompanyPeople = [{
"FirstName": "Person",
"LastName": "One",
"MemberType": {
"IsMember": false,
"Name": "Non-member"
}
}, {
"FirstName": "Second",
"LastName": "Person",
"MemberType": {
"IsMember": true,
"Name": "Member"
}
}, {
"FirstName": "Three",
"LastName": "People",
"MemberType": {
"IsMember": false,
"Name": "Non-member"
}
}];
$scope.registrantSelected = {
"FirstName": "Person",
"LastName": "One",
"MemberType": {
"IsMember": false,
"Name": "Non-member"
}
};
}]);
HTML:
<div ng-app="app" ng-controller="MainController">
<div class="col-xs-12 col-sm-5">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-6 control-label">Registration for</label>
<div class="col-sm-6">
<select class="form-control" ng-model="registrantSelected" ng-options="person.FirstName + ' ' + person.LastName for person in paidDuesCompanyPeople">
</select>
</div>
</div>
</form>
</div>
{{registrantSelected}}
</div>
感謝您的幫助,您可以給!
$ scope.registrantSelected是不一樣的對象...您需要使用paidDuesCompanyPeople [0]設置,或使用ng-options跟蹤來定義要在選擇器上進行比較的字段 –