What is $$hasKey in the array items?

Asked

Viewed 55 times

0

I realized that for some reason, a property called $$hasKey was added to my array items. Each item has this property with different values.

Example:

[0: {$$hasKey: 'Object1', 
    uf: 'ES'},
 1: {$$hasKey: 'Object2',
    uf: 'SP'}
]

In this example is an acronym list of Brazilian states, the object I load from the server does not have the property $$hasKey, and my array, come without this property up to my controller.

When debugging I could not find the time when this property is added to my items array.

I want to understand:

  • What is this
  • What good is
  • If you can remove
  • 1

    In my design: The property is only one way that the angular has found to be able to identify which item is which, because when you render an array of information on the screen, the front end has to have some way of being able to identify what is actually the element that is rendered and how it differs from other similar elements, this also helps to synchronise the already rendered information with some other new information to be rendered or removed. There must be a way you can customize the KEY for any of your preferences.

1 answer

1


What is this?

The estate $$HashKey is created internally by Angularjs (more explicitly by the internal function $id()) when the library needs to identify only members of a collection - as is the case of use of ng-repeat.

What good is?

So that Angularjs can map the members of collections individually.

When you use ng-repeat='item in items' without specifying a track by internally ng-repeat will apply track by $id(item). This function will calculate the hash in objects, or return the value directly in primitives.

Has to remove?

Yes. The native function angular.toJson() removes all internal properties - but remember that it returns a string, and you need to desirialize it back to an object via JSON.parse():

var objetoPuro = JSON.parse(angular.toJson(objetoComHashKey));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.