【发布时间】:2016-04-02 11:50:12
【问题描述】:
我有一个select,我想用ng-options 填充它。选项的来源不是array,而是object。到目前为止一切顺利。
这个select 也有ng-model。也就是说当模型的属性取值时,select需要根据取值来改变选中的option。
问题是选中的option 总是第一个(空`选项),就像在这个演示中一样:
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.resourceList = {"0":"Unknown","1":"ILS","2":"USD","3":"AUD","4":"GBP","5":"JPY"};
$scope.resources = {
type: 3
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<div ng-app="app" ng-controller="MainCtrl">
{{resources.type}}
<select ng-model="resources.type" ng-options="key as value for (key, value) in resourceList track by key"></select>
<button data-ng-click="resources.type = '4'">Replace</button>
</div>
更新:resources.type = '4' 和 resources.type = 4 我都试过了,它们都不起作用。
注意:我在这里阅读了很多类似主题的问题,但没有找到此特定问题的答案,所以请在将其标记为重复之前再次阅读该问题。
【问题讨论】:
-
尝试分配
1-data-ng-click="resources.type = '1'"的字符串表示 -
@tymeJV 我试过了,但没用。
-
不清楚问题是什么。当用户更改选择框中的值时,ng-model 会更改。它应该是一个字符串。当您单击按钮时,将分配类型,但请尝试使用字符串进行分配。
-
@SoluableNonagon 无论如何都没有(请参阅我的答案中的更新)
-
docs.angularjs.org/api/ng/directive/ngSelected 我想你正在寻找一个选定的属性
标签: javascript html angularjs angular-ngmodel ng-options