1

我有一个select标签,我正在应用角度选择。

这是我的select标签

<select name="rname" id="rname" ng-model="rname" ng-init="rname='CustomReport'" 
   ng-options="key as value for (key , value) in reportsValuesOptions track by key" chosen>
        <option value="">---Select---</option>
</select>

上面的select标签是从下面的对象中填充的

$scope.reportsValuesOptions = {'Cash Position Report':'Cash Position Report','Detail Report':'Detail Report','Reconciliation Report':'Reconciliation Report','Summary Report':'Summary Report','Sweep Report':'Sweep Report','FCCS/FBPS Detail Report':'FCCS/FBPS Detail Report','CustomReport':'Custom Report Name'};

select该对象是标签的一对值和选项,其中isthe is thekeyoptions tags valuevalueoption tag text

现在我想将select标签的默认值设置为'CustomReport'as itsoption value'Custom Report Name'as its option textfrom the above object,使用ng-init.

我试过做ng-init="rname='CustomReport'",但它不起作用

如何从对象的键值对中设置其值?

完整示例

4

2 回答 2

0

您可以像这样简单地使用 ng-init

<select ng-init="somethingHere = options[0]" 
        ng-model="somethingHere" 
        ng-options="option.name for option in options">
</select>
于 2016-08-05T07:47:20.920 回答
0

您的解决方案的问题是因为您提供了一个对象,而 AngularJS 主要是为数组设计的,这导致 AngularJS 无法正确跟踪它们。您可能想为它编写一个较短的对象,reportsValueOptions但它应该是一个对象数组,其形式类似于以下内容:

[
  {label: 'First Label', value:'first-option'},
  {label: 'Second Label', value:'second-option'}
]

这是带有修改对象的jsfiddle的修改版本,它还显示选择了哪一个。

您还可以在此处了解有关对象问题的更多信息:https ://docs.angularjs.org/api/ng/directive/ngOptions#complex-models-objects-or-collections-

于 2016-08-05T08:48:08.683 回答