最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get selected value in angular ng-option - Stack Overflow

programmeradmin0浏览0评论

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

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

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

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

Share Improve this question asked May 16, 2014 at 3:50 underscoreunderscore 6,9077 gold badges43 silver badges81 bronze badges 4
  • @MarcKline did you my fiddle ? – underscore Commented May 16, 2014 at 4:11
  • @MarcKline see the update fiddle jsfiddle/cQ99H/2 – underscore Commented May 16, 2014 at 4:13
  • @MarcKline NOOOO.IT showing selected array.I need the key.When selected HND i need to parse HND to the controller – underscore Commented May 16, 2014 at 4:16
  • e chat.stackoverflow./rooms/17/javascript – underscore Commented May 16, 2014 at 4:18
Add a ment  | 

2 Answers 2

Reset to default 1

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 bination will allow your users to select from the keys of data, and once they do, the second select box will contain the four letters associated with them in the associated value array.

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

Working Fiddle

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

Working Demo

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);
    }
}
发布评论

评论列表(0)

  1. 暂无评论