mr12086
mr12086

Reputation: 1147

AngularJS: is $$hashkey a reliable key

Question

I'm interested in the properties of $$hashkey on angular arrays/objects.

  1. Would each generated hashkey be the same each time you reload a page; a quick test tells me yes but I somewhat assumed it wouldn't?
  2. If you updated/added to the existing array, would the old hashkey's stay consistent?
  3. If the above is true, is there a way to fetch from an array using the hashkey? - of cause I could roll my own but before I recreate the wheel I thought I'd ask.

Example:

Views would include:

Fetch method:

angular.get($$hashkey);

You would then pass the hashkey of the element and it would return a reference to that array inside the full array.

Lastly the data would be:

{
    form_id: 1
    form_desc: 'xxx',
    form_name: 'name 1',
    Elements: [
        {
            element_id: 1,
            element_name: 'element1',
            default_value: null,
            disabled: "0",
            element_type: "image",
            ElementOptions: [
                {
                    show: false,
                    sort_order: 0,
                    value: "ar",
                },
                {
                    show: true,
                    sort_order: 1,
                    value: "rw",
                }
            ],
        },
        {
            element_id: 2,
            element_name: 'element2',
            default_value: null,
            disabled: "0",
            element_type: "image",
            ElementOptions: [
                {
                    show: false,
                    sort_order: 0,
                    value: "ar",
                },
                {
                    show: true,
                    sort_order: 1,
                    value: "rw",
                }
            ],
        }
    ]
}

Upvotes: 2

Views: 2286

Answers (2)

mccainz
mccainz

Reputation: 3497

$$hashkeys will only be computed for functions and objects so if you wish to track anything that isn't one of those types then you have that limitation.

$$Hashkeys look like ... (function||object): N ...

Where N is just an incremental value being adjusted + 1 for each $$HashKey computed. So, in many cases this could be the same value across page loads. But loading data that is asynch, will cause differences when multiple data sources are being queried as part of page initialization and order of return cannot be guranteed. In cases like that you would have to marshall all your asynch data and then assign that data to your scope in a specific order to ensure consistent $$hashkeys.

Moving items around in an array that is linked to our DOM (via ng-repeat) will not change that items $$hashkey. Deleting it and re-adding it will.

I would not use $$Hashkey for my own housekeeping as it is intended to be internal to AngularJS.

Upvotes: 4

Miraage
Miraage

Reputation: 3464

I've used this internal private property when I had no identifiers. I think, it's pretty usable, but not recommended.

Upvotes: 2

Related Questions