Serhiy Serhiy - 2 months ago 8
AngularJS Question

How to use $$hashKey in my app?

How to use $$hashKey in app.
The situation is next:
I make a call to server to receive array of objects, than I pass the array to ng-repeat:

function listWorkItems(data) {
if (data.ok) {
vm.workItems = data.content.items;

}
}


than every second I make a call to server and want to check if received array (data.content.items) differ from listed in DOM (vm.workItems);

to check it I use:

for(var i=0; i<data.content.items.length; i++) {
if(JSON.stringify(data.content.items[i]) !== JSON.stringify(vm.workItems[i])) {
vm.workItems[i] = data.content.items[i];
}
}


But I always receuiev NOT equal, because in vm.workItems there is $$hashkey.
what to do?

Answer

Use, angular.toJson instead of JSON.stringify and try it.

(var i=0; i<data.content.items.length; i++) {
   console.log(angular.toJson(data.content.items[i]))
   console.log(angular.toJson(vm.workItems[i]))
   if(angular.toJson(data.content.items[i]) !== angular.toJson(vm.workItems[i])) {
       vm.workItems[i] = data.content.items[i];
   }

Using angular.toJson, angular removes such sort of extra values.