【问题标题】:angular material md-select using track by but still getting $$hashKey角度材料 md-select 使用 track by 但仍然获得 $$hashKey
【发布时间】:2016-03-05 17:22:28
【问题描述】:

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

vm.productTypes 是任何具有 id 属性的对象数组。

生成的模型值...

$$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

【问题讨论】:

    标签: angularjs angular-material angularjs-track-by


    【解决方案1】:

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

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

      Link

    【讨论】:

      【解决方案2】:
              <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>
      

      【讨论】:

      • 因为你默认使用 ng-repeat,所以 Angular 会添加 $$hashKey。如果我们需要简单地删除它,我们可以通过 $index 或 id 或任何键使用 track,
      • 我已经通过 id 属性进行跟踪。查看我所做的评论,似乎当名称属性位于 md-select 上时,它仍然添加了 $$hashKey。您的回答并没有真正提供任何关于 name 属性为什么会导致这种行为的新信息。
      • 嗨,哥们,当我运行上面给出的代码笔链接时,我会看到错误消息,“SyntaxError: missing ; before statement angular-material.js:1:4 Error: [$injector:modulerr] Failed实例化模块 MyApp 由于:[$injector:modulerr] 无法实例化模块 ngMaterial 由于:[$injector:nomod] 模块 'ngMaterial' 不可用!您拼错了模块名称或忘记加载它。如果注册一个模块确保您将依赖项指定为第二个参数。errors.angularjs.org/1.4.8/$injector/nomod?p0=ngMaterialminErr loadModules/ajax.
      猜你喜欢
      • 2016-04-12
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 2015-06-01
      • 2017-12-29
      • 1970-01-01
      相关资源
      最近更新 更多