Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-render angular filters

Tags:

angularjs

Is there a way to re-render AngularJS filters? I am trying to use the angular currency filter that displays the currency symbol based on the language file that's loaded. I need to re-render the filters once I programmatically load the appropriate language file.

like image 735
Sarvesh Avatar asked Jan 03 '14 08:01

Sarvesh


1 Answers

From Angular 1.3 filters are stateless which means filters just will update when theirs input have change.

If you want to update your filter ever, you need to make your filters $stateful.

app.filter('translate', translate);

translate.$inject = ['$rootScope'];

function translate($rootScope){

    filter.$stateful = true;

    return filter;

    function filter(str) {
        return i18n[$rootScope.currentLang][str];
    };
}

Filters will execute on each $digest but this is inadvisable by perfm.

like image 90
andy Avatar answered Oct 19 '22 20:10

andy