对象的ng-options,如何获取键值以实现功能(angularJS)

时间:2015-07-01 11:40:40

标签: angularjs select ng-options

//.js
    $scope.scopeObject = {
      "a":{},
      "b":{},
      "c":{}.......
    }
//html
    <div>
    <select ng-model="scopeModal" ng-options="key for (key,value) in scopeObject" ng-change = "getKey(group)"></select>
    </div>
//js
$scope.getKey = function(group){
 console.log(group)//undefined
}

在这种情况下,组的功能未定义... 我如何获得我的功能的关键价值? TY

1 个答案:

答案 0 :(得分:0)

您应该在更改事件中将scopeModal传递给您的函数。您可以查看以下代码段。

&#13;
&#13;
angular.module("myApp", [])
  .controller("myCtrl", ["$scope", function($scope) {
    $scope.scopeObject = {
      "a": {id: "a", value: "a"},
      "b": {id: "b", value: "b"},
      "c": {id: "c", value: "c"}
    }
    $scope.getKey = function(group) {
      console.log(group) //undefined
    }
  }]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <select ng-model="scopeModal" ng-options="key for (key,value) in scopeObject" ng-change="getKey(scopeModal)"></select>
  {{scopeModal}}
</div>
&#13;
&#13;
&#13;