2016-09-30 31 views
0

我有問題ngStorage。當我使用我的工廠jQuery的$ajax,我不能使用$localStorage.then()(它不工作)。

$scope.updateProfile = function() {   
    updateProfileFactory.init($scope.dataProfile).then(   
     function (data) 
      if (data.status == "success") { 
       $localStorage.currentUser.fullname = $scope.dataProfile.u_fullname; 
      }     
     }   
    ) 
} 

如果我使用$localStorage.then(),它的工作的罰款,但我必須把它放在裏面。

$scope.updateProfile = function() {   
    $localStorage.currentUser.fullname = $scope.dataProfile.u_fullname; 
    updateProfileFactory.init($scope.dataProfile).then(
     function (data) { 
      if (data.status == "success") { 
       // code 
      }    
     }   
    ) 
} 

問題在哪裏?

+0

請儘量定義「不起作用」。你可以調試,看看'then'函數是否曾被稱爲? –

回答

3

爲什麼不使用Angular的$http庫來做你的AJAX的東西?混合jQuery和Angular證明會產生不可預知的結果。

我可以看到您已經在您的應用程序中使用了Angular。所以你沒有理由不完全採用Angular的方式。 $http服務提供了jQuery的$.ajax提供的所有功能以及更多功能。

當你在vanilla Javascript或jQuery函數中使用Angular函數時,Angular不知道這些被調用,因爲它們在它的範圍之外。在這裏你可以使用Angular的$apply,但這並不理想,而且很冒險。

這裏您的問題可能是$.ajax的回調函數不知道$localStorage,因爲它不作爲依賴項傳入。

+0

我使用$ .ajax becouse $ http「POST」對標題和數據格式有默認問題。這很容易。 –

+0

如果您可以發佈您遇到的問題,我們可以幫助您。 '$ http'不能做$'.ajax'所能做的。頭部可以設置,並且您可以完全控制有效載荷 – nikjohn

+0

我有項目,其中使用$ .ajax是更快,更簡單,更好的方法。我的問題不是關於$ .ajax vs $ http,所以你回答沒有幫助。 –

相關問題