Angularjs 在ngRepeat中自动包含$$hashKey
我一直在寻找解决方案,我尝试了所有的方法,但没有找到解决方案 在我的代码中,我通过ajax从数据库中获取一个数组 我得到了正确的数组,如下所示:Angularjs 在ngRepeat中自动包含$$hashKey,angularjs,Angularjs,我一直在寻找解决方案,我尝试了所有的方法,但没有找到解决方案 在我的代码中,我通过ajax从数据库中获取一个数组 我得到了正确的数组,如下所示: [{"id":"1","name":"product 1","image":null},{"id":"2","name":"product 2","image":null},{"id":"3","name":"product 3","image":null}]; 但是当我在ng repeat中使用它时,它会添加$$hashKey。因此,我的vendo
[{"id":"1","name":"product 1","image":null},{"id":"2","name":"product 2","image":null},{"id":"3","name":"product 3","image":null}];
但是当我在ng repeat
中使用它时,它会添加$$hashKey
。因此,我的vendor.js文件中出现错误:
未捕获的TypeError:无法读取未定义的属性“nodeName”
我都试过了
angular.copy($scope.myArray);
angular.toJson($scope.myArray);
JSON.stringify($scope.myArray);
我也尝试过:
<div ng-repeat="smallArray in myArray track by $id($index)">
等于
$scope.ans = '[{"id":"1","name":"product 1","image":null},{"id":"2","name":"product 2","image":null},{"id":"3","name":"product 3","image":null}]';
这是我的html
<div class="slide-wrap" ng-repeat="smallArray in myArray">
<div class="slide__content-wrap">
<h1 class="title">{{smallArray.name}}</h1>
</div>
<div class="slide__content-wrap__inside show-for-large-up">
<p class="description">{{smallArray.image}}</p>
<a class="btn btn--transparent--red" href="/premium-ice-cream">view
all flavors</a>
</div>
</div>
{{smallArray.name}
{{smallArray.image}
任何人都可以告诉我如何在删除
$$hashKey
的情况下获得数组。如果ng repeat
指令中没有设置track by
的话,AngularJS会向对象添加$$hashKey
属性来跟踪更改。只需尝试使用ng repeat=“item in data track by item.id”
即可。通过这种方式,AngularJS将此“唯一”值用于其内部跟踪
看法
提供您的
ng repeat
HTML此处添加了我的HTML部分任何反馈m8?感谢您的解决方案,兄弟。
<div class="slide-wrap" ng-repeat="smallArray in myArray">
<div class="slide__content-wrap">
<h1 class="title">{{smallArray.name}}</h1>
</div>
<div class="slide__content-wrap__inside show-for-large-up">
<p class="description">{{smallArray.image}}</p>
<a class="btn btn--transparent--red" href="/premium-ice-cream">view
all flavors</a>
</div>
</div>
<div ng-controller="MyCtrl">
<div ng-repeat="item in data track by item.id">
{{ item | json }}
</div>
<br />
<div ng-repeat="item in hashData">
{{ item | json }}
</div>
<br />
<br />
<button ng-click="track()">
Click me to log
</button>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.data = [{
"id": "1",
"name": "product 1",
"image": null
}, {
"id": "2",
"name": "product 2",
"image": null
}, {
"id": "3",
"name": "product 3",
"image": null
}];
$scope.hashData = [{
"id": "1",
"name": "product 1",
"image": null
}, {
"id": "2",
"name": "product 2",
"image": null
}, {
"id": "3",
"name": "product 3",
"image": null
}];
$scope.track = function () {
console.log($scope.data);
console.log($scope.hashData);
}
});