说我有数据库中的这些选项
$scope.options = [
{ id: 192, value: 'Bank 1'},
{ id: 168, value: 'Bank 2'},
{ id: 0, value: 'Bank 3'},
];
并且此代码适用于通过选择数组位置
来设置默认值<select ng-init="data.bankoptions=options[0]" ng-model="data.bankoptions" ng-options="bank.label for bank in options track by bank.id">
并将Bank 1显示为默认值
如何根据对象ID设置默认选择?选择192,168或0。
谢谢!
答案 0 :(得分:1)
尝试使用select
as
表达式
ng-options="bank.id as bank.label for bank in options"
答案 1 :(得分:1)
var data = {
availableoptions: [],
selectedoption: {}
};
将数据添加到选项
data.availableoptions.push({id:192,label:&#39; Bank1&#39;});
设置默认选择
data.selectedoption = {id:192}
获取默认选择
$ scope.default = data.selectedoption;
选择标记
<select ng-model="default" ng-options="bank.label for bank in data.availableoptions track by bank.id">