我使用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>
答案 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);
}