javascript 获取角度 ng-option 中的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23692349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:24:13  来源:igfitidea点击:

get selected value in angular ng-option

javascriptangularjs

提问by underscore

I have an object like bellow:

我有一个像下面这样的对象:

var data = {
    BCS: ['a', 'b', 'c', 'd'],
    HND: ['e', 'f', 'g', 'h'],
    LMU: ['i', 'j', 'l', 'm']
};

And Bellow is my controller

贝娄是我的控制器

function getDivision($scope) {
    $scope.divisions = data;
}

And this is model

这是模型

<div ng-app>
    <div ng-controller='getDivision'>
        <select ng-model="key" ng-options="key for (key , val) in divisions" ng-change="update()"></select>
        <select ng-model="divisionValues" ng-options="value for value in key"></select>
    </div>
</div>

This is JsFiddle

这是JsFiddle

How can i get first selected value and parse it to the controller ?

如何获得第一个选定的值并将其解析到控制器?

采纳答案by Marc Kline

Your expressions should look like the following:

您的表达式应如下所示:

<select ng-model="key"ng-options="key as key for (key, val) in divisions"
ng-change="update()"></select>

<select ng-model="divisionValues"
ng-options="value for value in divisions[key]"></select>

This combination will allow your users to select from the keys of data, and once they do, the second selectbox will contain the four letters associated with them in the associated value array.

此组合将允许您的用户从 的键中进行选择data,一旦选择,第二个select框将包含关联值数组中与它们关联的四个字母。

The expressions are tricky and hard to understand at first, but if you read the docsa few timesand experiment, it will all make sense.

这些表达式一开始很棘手且难以理解,但是如果您多读几遍文档并进行实验,就会明白一切。

Working Fiddle

工作小提琴

回答by Nidhish Krishnan

You have to pass the key to the update method like ng-change="update(key)"

您必须将密钥传递给更新方法,例如 ng-change="update(key)"

Working Demo

工作演示

html

html

<div ng-app>
    <div ng-controller='getDivision'>
        <select ng-model="key" ng-options="key as key for (key, val) in divisions" ng-change="update(key)"></select>
        <select ng-model="divisionValues" ng-options="value for value in divisions[key]"></select>
    </div>
</div>

script

脚本

var data = {
    BCS: ['a', 'b', 'c', 'd'],
    HND: ['e', 'f', 'g', 'h'],
    LMU: ['i', 'j', 'l', 'm']
};

function getDivision($scope) {
    $scope.divisions = data;
    $scope.update = function (a) {
        console.log(a);
    }
}