如果下拉列表与json键绑定,如何从下拉列表中获取选定值

how to get selected value from Drop-down, if drop-down is binded with keys of json

本文关键字:下拉列表 获取 json 绑定 如果      更新时间:2023-09-26

我对Angularjs很陌生,我已经将列名(键值对中的键)绑定到了select列表中,并希望在更改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>

<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);
  };
});

事实上,它就在文档中。。。

这是一个灌篮器。