如何确定不同的执行何时结束,以便在AngularJs中加载数据?
我使用本地化cookie跟踪当前的文化。我希望在应用程序启动时加载当前所选区域性的所有消息,并将其与区域性信息一起保存在如何确定不同的执行何时结束,以便在AngularJs中加载数据?,angularjs,localization,local-storage,deferred-execution,Angularjs,Localization,Local Storage,Deferred Execution,我使用本地化cookie跟踪当前的文化。我希望在应用程序启动时加载当前所选区域性的所有消息,并将其与区域性信息一起保存在localStorage中,然后仅在所选区域性更改时刷新它们(当localStorage中的'CurrentCulture'的值与cookie中的值不同时) 在我的控制器中,我设置 $scope.messages=LocalizationService.messages(LocalizationMessagesURL,currentCulture); 在本地化服务中,我有
localStorage
中,然后仅在所选区域性更改时刷新它们(当localStorage
中的'CurrentCulture'
的值与cookie中的值不同时)
在我的控制器中,我设置
$scope.messages=LocalizationService.messages(LocalizationMessagesURL,currentCulture);
在本地化服务中,我有
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
在GetLocalizationsFromServer
完成之前返回undefined
,因此在第一次加载时我没有任何消息。如果我刷新页面,那么我就可以正确获取它们。我如何解决这个问题?getCachedMessagesForLanguage
返回一个延迟的值
然后你可以做
getCachedMessagesForLanguage(localisationMessagesUrl,
language).then(function(Localisation) {
return getLocalisationsFromServer(localisationMessagesUrl)
}).then(function(responseFromGetLocalisationsFromServer) {
// do your thing here
})