我对 Angularjs 非常陌生,我已将列名称(键值对中的键)绑定(bind)到选择列表,并希望在选择的选择更改时获取键名称。 显示选择列表:
姓名、摘要和年龄
在选择更改时,我想要选择的选定文本。请帮忙 这是我的代码:
<!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>
最佳答案
<select ng-options="key as key for (key,value) in phones[0]" ng-change="ChangeValue()" ng-model="selectedVal"></select>
Controller :
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 .
关于javascript - 如果下拉列表与 json 的键绑定(bind),如何从下拉列表中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26449092/