1

I using Angularjs, and trying to get in controller, from view, selected options (key and value) in select.

How to get - key, I know, but how to get key and value?

View:

<select
    ng-change="Main.getLocalityByAddress(regions)"
    ng-model="regions"
    ng-options="data.id as data.name for data in Main.regions">

    <option value>{{"Select region" | localization:'index'}}</option>
</select>

Controller:

Main.getLocalityByAddress = function(selected_region) {
}

Upd:

Whats wrong?:

Controller

app.controller('Main', function($scope){
    var Main = {};

    $scope.getLocalityByAddress = function() {
        console.log($scope.regions); // undefined
    };

    $scope.Main = Main;
});

View

<select
    ng-change="getLocalityByAddress()"
    ng-model="regions"
    ng-options="data.name for data in Main.regions">
</select>
2
  • What is Main.regions? Commented Dec 26, 2013 at 17:41
  • There stored my regions, Main - Controller Commented Dec 28, 2013 at 8:19

1 Answer 1

4

You can bind the entire object to model.

<select
    ng-change="changed()"
    ng-model="regions"
    ng-options="data.name for data in options">
</select>

And in the controller;

$scope.changed = function() {
    console.log($scope.regions);
}

Working Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.