如何从Controller Angularjs中的选定选项中获取键和值

时间:2013-12-26 14:57:30

标签: angularjs select

我使用Angularjs,并尝试从视图中选择控制器,选择选择的选项(键和值)。

如何获得 - 关键,我知道,但如何获得关键和价值?

查看:

<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>

控制器:

Main.getLocalityByAddress = function(selected_region) {
}

UPD:

什么错了?:

控制器

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

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

    $scope.Main = Main;
});

查看

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

1 个答案:

答案 0 :(得分:4)

您可以将整个对象绑定到模型。

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

在控制器中;

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

Working Fiddle