1

我试图摆脱 angular 添加到模型值的 $$hashKey 值。根据大多数实施跟踪的消息来源,应该可以解决这个问题,但我做错了。

vm.productTypes 是 id 属性为 GUID 的任何对象数组。

生成的模型值...

$$hashKey: "object:445"
id: "9e695340-d10a-40ca-9cff-e9a93388912a"
name: "Medical"
type: 1
typeString: "ProductTypes"

HTML 代码:

<md-select id="type" ng-model="vm.currentProduct.productType" name="type"
                           ng-model-options="{trackBy: '$value.id'}"
                           required>
                    <md-option ng-repeat="pt in vm.productTypes track by pt.id" ng-value="pt">
                        {{pt.name}}
                    </md-option>
                </md-select>

我哪里错了?

更新:

似乎 name 属性导致了这种奇怪的行为。漏洞? http://codepen.io/anon/pen/LNpMYJ

4

2 回答 2

2

使用ng-model-options="{ trackBy: '$value.id' }".

  • 如果通过$http调用获取列表数据,首先准备model对象,然后加载列表数据。
  • 或准备模型对象并放入保存孔形数据的对象中

    链接

于 2017-04-19T11:19:07.690 回答
0
        <html>
<head>
<title>$$HaskKey Remover</title>
    <script src="https://code.angularjs.org/1.3.8/angular.min.js></script>
        <script>
        var myApp= angular.module('MyApp', []);
        myApp.controller('MainCtrl', ['$scope',
          function($scope) {
         $scope.list = [
              {key: "1", name: "Rose"},
              {key: {id:2}, name: "Sachin"},
              {key: {id:3}, name: "Sandy"}
            ];
           console.log($scope.list);
          }
        ]);
        </script>
        <head>
        <title>Removing $$hashKey when using ng-options</title>
        </head>
        <body ng-app='MyApp'>
            <div ng-controller='MainCtrl'>
        <form>   
           <label for="Select Box">Make a choice of Players:</label>
            <select name="selectBx" id="selectBx" ng-model="optionsData"
               ng-options="item.name for item in list track by item.key">
              </select>
        </form>
          </div>
        </body>

        </html>
于 2016-03-05T19:20:13.137 回答