2017-10-18 12 views
1

저는 현지화 쿠키를 사용하여 현재 문화를 추적하고 있습니다. 응용 프로그램이 시작될 때 현재 선택한 문화권의 모든 메시지를로드하고 해당 문화권에 대한 정보와 함께 localStorage에 보관 한 다음 선택한 문화권이 변경 될 때만 새로 고침 (localStorage의 값이 'CurrentCulture' 인 경우에만 새로 고침합니다. 쿠키에서). AngularJs에서 데이터를로드 할 수 있도록 다른 실행이 종료 될 때를 어떻게 확인할 수 있습니까?

내 컨트롤러에서 나는 첫 번째로드에 내가 메시지가 없습니다 그래서, getLocalisationsFromServer 완료하기 전에

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 반환 undefined을 가지고 localisationService에서 $scope.messages = localisationService.messages(localisationMessagesUrl, currentCulture);

을 설정합니다. 페이지를 새로 고침하면 제대로 표시됩니다. 이 문제를 어떻게 해결할 수 있습니까?

답변

1

메이크업은 .. 그런 다음 할 수있는 deferred.promise

을 반환 getCachedMessagesForLanguage

getCachedMessagesForLanguage(localisationMessagesUrl, 
language).then(function(Localisation) { 
    return getLocalisationsFromServer(localisationMessagesUrl) 
}).then(function(responseFromGetLocalisationsFromServer) { 
    // do your thing here 
})