我是Angularjs的新手,我已将列名(键值对中的键)绑定到选择列表,并希望在更改select的选择时获取键名。 选择列表显示:
姓名,摘要和年龄
在选择更改时,我想要选择的所选文本。请帮忙 这是我的代码:
<!DOCTYPE html>
<div ng-controller="HelloController">
<h2>Hello {{helloTo.title}} !</h2>
<select ng-options="key for (key,val) in phones[0]" ng-change="ChangeValue(key)" ng-model="phones"></select>
</div>
<script>
angular.module("myapp", [])
.controller("HelloController", function ($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "World, AngularJS";
$scope.phones = [
{
'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.',
'age': 1
},
{
'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.',
'age': 2
},
{
'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.',
'age': 3
}
];
$scope.ChangeValue = function (selectedval) {
alert(selectedval.name);
};
});
</script>
答案 0 :(得分:1)
<select ng-options="key as key for (key,value) in phones[0]" ng-change="ChangeValue()" ng-model="selectedVal"></select>
控制器:
angular.module("myapp", [])
.controller("HelloController", function ($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "World, AngularJS";
$scope.selectedVal = '';
$scope.phones = [{
'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.',
'age': 1
}, {
'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.',
'age': 2
}, {
'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.',
'age': 3
}];
$scope.ChangeValue = function() {
console.log($scope.selectedVal);
};
});
它实际上在documentation ......
这里是plunker。