【发布时间】:2018-03-30 17:56:57
【问题描述】:
我正在使用本地化 cookie 跟踪当前的文化。我想在应用程序启动时加载当前所选文化的所有消息并将它们与有关其文化的信息一起保存在localStorage 中,然后仅在所选文化更改时刷新它们(当@987654323 中'CurrentCulture' 的值时@ 与 cookie 中的不同)。
在我的控制器中,我设置
$scope.messages = localisationService.messages(localisationMessagesUrl, currentCulture);
在localisationService,我有
app.service("localisationService", function ($q, $http, localStorageService) {
var getCachedMessagesForLanguage = function (localisationMessagesUrl, language) {
console.log('getCachedMessagesForLanguage');
var messages = localStorageService.get('Localisation');
if (!messages || language !== localStorageService.get('Language')) {
getLocalisationsFromServer(localisationMessagesUrl).then(function (serverMessages) {
localStorageService.add('Localisation', messages);
localStorageService.add('Language', language);
});
}
return localStorageService.get('Localisation')
}
function getLocalisationsFromServer(localisationMessagesUrl) {
return $q(function (resolve) {
$http.get(localisationMessagesUrl)
.then(function (response) {
resolve(response.data);
});
});
}
return {
messages: function (localisationMessagesUrl, language) {
return getCachedMessagesForLanguage(localisationMessagesUrl, language);
}
};
});
getCachedMessagesForLanguage 在getLocalisationsFromServer 完成之前返回undefined,所以在第一次加载时我没有任何消息。如果我刷新页面,那么我会正确获取它们。我该如何解决这个问题?
【问题讨论】:
标签: angularjs localization local-storage deferred-execution