“10 $digets() iterations reached” when trying to $

2019-08-10 01:12发布

问题:

We're using angular translate to handle localization of a web site, using our own custom loader that fetches translations from the server. On the server side, we have logic that handles missing translation keys so that we both return something (a translation from a fallback language or the key itself) and reports to the system that the key was requested (so it shows up in the translation UI elsewhere on the site). Now, I'm struggling with building something similar on the client side.

The $translateProvider has a method useMissingTranslationHandler(factory) which we've successfully configured to just log out that a key is missing to the console, using the following:

app.config(function ($translateProvider) {
    $translateProvider.useMissingTranslationHandler('translationMissingHandler');
});

app.factory('translationMissingHandler', translationMissingHandler);

translationMissingHandler.$inject = ['$window', '$log', '$http'];
function translationMissingHandler($window, $log, $http) {
    return function (translationId) {
        var culture = $window.preferredCulture, // workaround for another problem
            errorInfo = {
                key: translationId,
                culture: culture
            };
        $log.warning('Translation missing:', errorInfo);
        // $http.post('/api/v2/localization/missing', errorInfo);
    }
}

However, then I uncomment the POST to the server, notifying about the missing key, the page hangs on line 14394 of angular.js - throw e, where e.message is

[$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []

I've tried various things to get around this - e.g. wrapping the call to $http.post() in $timeout or $rootScope.$apply but to no avail; I still get the same error message.

Is there a way to schedule a call to $http.post() from here without causing this error? If so, how?