[英]AngularJS How to initialize a select using ng-options with an object value
我想用ng-options
初始化選擇字段ng-options
。
假設我們有以下各項:
$scope.items = [{
id: 1,
label: 'aLabel',
subItem: { name: 'aSubItem' }
}, {
id: 2,
label: 'bLabel',
subItem: { name: 'bSubItem' }
}];
以下html:
<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select>
但是現在,而不是通過以下命令選擇選項:
$scope.selected = $scope.items[0];
我沒有對象,我只有id的信息,所以我喜歡用類似這樣的方法初始化選擇:
$scope.selected = $scope.otherRandomObject.id
我的想法是使用track by item.id
在某種程度上創建了對象和id之間的關系。
如果要具有預選值,則可以使用ng-selected
<select ng-options="item as item.label for item in items" ng-selected="$first"></select>
在這種情況下,將選擇項目數組中的第一個項目
希望我回答了你的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.