Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs 1.3 async filter not working

I have following code, which basically loads messages from the server via $http.get request in the service and then used in the i18n filter. Filter was working fine with angular version 1.2.24 but after updating to 1.3.5 it no longer works.

I was wondering if anyone ran into similar issue and can shine some light on this.

var module = angular.module('myApp', [])

.factory('MessageFactory', function ($http, $locale, $log) {
    var messages = {};

    $http.get("/i18n/" + $locale.id + "/list", {cache: true}).success(function(data) {
        $log.debug("Getting messages for", $locale.id);
        messages = data;
    });

    return {
        getMessage: function (key) {
            return messages[key];
        }
    }
})

.filter('i18n', function (MessageFactory) {
    return function (key) {
        return MessageFactory.getMessage(key);
    }
});

Html Code

<h2>{{'message.page.title'|i18n}}</h2>
like image 286
anazimok Avatar asked Dec 10 '14 13:12

anazimok


1 Answers

Finally after couple of hours of digging I changed

.filter('i18n', function (MessageFactory) {
    return function (key) {
        return MessageFactory.getMessage(key);
    }
});

to

.filter('i18n', function (MessageFactory) {
    function filterFn(key) {
        return MessageFactory.getMessage(key);
    }

    filterFn.$stateful = true;

    return filterFn;
});

Notice filterFn.$stateful that's what did the trick.

like image 153
anazimok Avatar answered Oct 05 '22 21:10

anazimok