我想在HTML页面上设置下拉框的值。我正在使用angular,我从REST调用中获得了预期的值。
我页面上的文本框等其他字段只需说出ng_mode_textId = myNewVal
即可获得更新后的值。
它不适用于<select>
代码。怎么做?
答案 0 :(得分:4)
在AngularJS中为选择组件指定可用选项:
<select ng-options="pair.id as pair.label for pair in myArray" ng-model="selectedId" />
您应该将$scope.myArray
设置为[(1, "one"), (2, "two"), (3, "three")]
,并将$scope.selectedId
,1
,2
或3
设为读/写{ work(在包含此select的控制器中进行这些更改):
mymodule.controller(['$scope', function($scope) {
$scope.myArray = [(1, "one"), (2, "two"), (3, "three")];
$scope.selectedId = 1; //lets give him a default selected value
$scope.someHowYouWillCallThisFunction = function() {
console.log("The selected id is: " + $scope.selectedId);
}
}])
或来自对象的键:值对:
<select ng-options="key as value for (key, value) in myObject" ng-model="selectedId" />