lavinjj / angularjs-localizationservice

AngularJS Resource Localization Service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why 'localize' service is always running

gudh opened this issue · comments

commented

Thanks for your post, it's amazing.

And I have one question, that is it seems 'localize' service is always running

snip20131009_5

it keep log out.

I put one line in localize.js, getLocalizedString function

console.log('run getLocalizedString');

the entire function is like so:

// checks the dictionary for a localized resource string
getLocalizedString: function(value) {
    // default the result to an empty string
    var result = '';

    // make sure the dictionary has valid data
    if ((localize.dictionary !== []) && (localize.dictionary.length > 0)) {
        // use the filter service to only return those entries which match the value
        // and only take the first result
        var entry = $filter('filter')(localize.dictionary, function(element) {
                return element.key === value;
            }
        )[0];

        console.log('run getLocalizedString');

        // set the result
        result = entry.value;
    }
    // return the value to the call
    return result;
}

You must be using the filter?

This is due to the fact that the directive is only called once per instance where a filter is re-evaluated each time the DOM is compiled.

If you use the markup data-i18n="...", it won't continuously run

commented

Why? I am curious, is it a bug? Can I fix it?

It's not a bug, what you are seeing is the digest loop in angular run every time data changes within your model. That was one of the reasons I went away from the filter because it gets called each time the digest loop is called which makes the filter a hog. As kdo mentioned above, using the directives resolves this problem, because they only update when the data int he attribute changes which is much more infrequent.

In my production code I've created several different directives to handle cases like placeholders what not to get around using the filter altogether.

Hope this helps.

commented

@lavinjj Thanks so much, I have switched to directive now :)

Works great ;D