2016-03-05 68 views
0

我想摆脱角度添加到您的模型值的$$ 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

回答

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> 
+0

由于您使用ng-repeat默认情况下会为其添加$$ hashKey。如果我们需要删除它,我们可以使用$ index或id或任何键的轨迹, – 2016-03-05 19:27:45

+0

我已经通过id属性进行跟踪。看看我的评论,似乎当名称属性在md-select上时,它仍然会添加一个$$ hashKey。你的回答并不真正给出任何新的信息,为什么name属性导致这种行为。 – Tersius

+0

嗨好友,当我运行代码笔链接给出上面显示我的错误消息,“SyntaxError:缺少;语句前 angular-material.js:1:4 错误:[$ injector:modulerr]无法实例化模块MyApp应付到: [$ injector:modulerr]由于以下原因,无法实例化模块ngMaterial: [$ injector:nomod]模块'ngMaterial'不可用!模块名称拼写错误或忘记加载模块名称如果注册模块确保您指定依赖关系作为第二个参数。 http://errors.angularjs.org/1.4.8/$injector/nomod?p0=ngMaterial minErr loadModules/<@https:// ajax。 – 2016-03-06 07:06:24

1

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

  • 如果您是通过电话$http获取列表数据,首先准备model对象,然后加载列表数据。
  • 或准备模型对象并投入该保持孔形式数据

    Link的对象。