Angular uses the $$hashKey to keep track of which elements in the DOM belong to which item in an array that is being looped through in an ng-repeat. Without the $$hashKey Angular would have no way to apply changes the occur in the JavaScript or DOM to their counterpart, which is one of the main uses for Angular.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj); The result will be a string following the JSON notation.
As a function, it takes two parameters: the key and the value being stringified. The object in which the key was found is provided as the replacer 's this parameter. Initially, the replacer function is called with an empty string as key representing the object being stringified.
Errors and Edge CasesJSON. stringify() throws an error when it detects a cyclical object. In other words, if an object obj has a property whose value is obj , JSON. stringify() will throw an error.
Angular adds this to keep track of your changes, so it knows when it needs to update the DOM.
If you use angular.toJson(obj)
instead of JSON.stringify(obj)
then Angular will strip out these internal-use values for you.
Also, if you change your repeat expression to use the track by {uniqueProperty}
suffix, Angular won't have to add $$hashKey
at all. For example
<ul>
<li ng-repeat="link in navLinks track by link.href">
<a ng-href="link.href">{{link.title}}</a>
</li>
</ul>
Just always remember you need the "link." part of the expression - I always tend to forget that. Just track by href
will surely not work.
In my use case (feeding the resulting object to X2JS) the recommended approach
data = angular.toJson(source);
help to remove the $$hashKey
properties, but the result could then no longer be processed by X2JS.
data = angular.copy(source);
removed the $$hashKey
properties as well, but the result remained usable as a parameter for X2JS.
It comes with the ng-repeat directive usually. To do dom manipulation AngularJS flags objects with special id.
This is common with Angular. For example if u get object with ngResource your object will embed all the resource API and you'll see methods like $save, etc. With cookies too AngularJS will add a property __ngDebug.
If you don't want to add id's to your data, you could track by the index in the array, which will cause the items to be keyed by their position in the array instead of their value.
Like this:
var myArray = [1,1,1,1,1];
<li ng-repeat="item in myArray track by $index">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With