下拉选择跟踪Id Angularjs

Drop down selection track by Id Angularjs

本文关键字:Id Angularjs 跟踪 选择      更新时间:2023-09-26

我有以下数据下拉在我的控制器。

 $scope.groups={{"gpId": 3, "name" :"Tom"}, {"gpId": 32, "name" :"Helen"},{"gpId": 9, "name" :"Amy"}
 $scope.user=
 {
   tkId: 32;
   place: NW
  }

在我的html中,我有以下选择

   <select ng-model="user.tkId" ng-options="a.gpId as a.name for a in groups track by a.gpId></select>

当我运行这个,我得到下拉Helen选定,但当我想从下拉改变选择,它不让我。请让我知道我如何改变它来选择其他选项,这样我就可以保存它,如果需要的话。由于

下面的代码片段可以工作。

首先,对象数组写成如下[{}, {}]

在angular文档中写着track byas不应该一起使用。

不要在同一个表达式中使用select astrack by。它们不是一起工作的

<body ng-app="selectExample">
  <script>
    angular.module('selectExample', [])
      .controller('ExampleController', ['$scope', function($scope) {
        $scope.groups = [{
          "gpId": 3,
          "name": "Tom"
        }, {
          "gpId": 32,
          "name": "Helen"
        }, {
          "gpId": 9,
          "name": "Amy"
        }];
        $scope.user = {
          tkId: 32,
          place: 'NW'
        };
      }]);
  </script>
  <div ng-controller="ExampleController">
    <label>Group:
      <select ng-model="user.tkId" ng-options="group.gpId as group.name for group in groups"></select>
    </label>
    <br/> {{user.tkId}}
  </div>
</body>