如何在Angular中将值设置为下拉列表

时间:2015-12-16 20:32:02

标签: javascript html angularjs

我想在HTML页面上设置下拉框的值。我正在使用angular,我从REST调用中获得了预期的值。

我页面上的文本框等其他字段只需说出ng_mode_textId = myNewVal即可获得更新后的值。

它不适用于<select>代码。怎么做?

1 个答案:

答案 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.selectedId123设为读/写{ 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" />