[英]ngStorage ($localStorage) dosen't work in .then()
我對ngStorage有問題。 當我將工廠用於jQuery的$ajax
,無法在.then()
使用$localStorage
(它不起作用)。
$scope.updateProfile = function () {
updateProfileFactory.init($scope.dataProfile).then(
function (data)
if (data.status == "success") {
$localStorage.currentUser.fullname = $scope.dataProfile.u_fullname;
}
}
)
}
如果我在.then()
之外使用$localStorage
,它可以正常工作,但是我必須將其放入其中。
$scope.updateProfile = function () {
$localStorage.currentUser.fullname = $scope.dataProfile.u_fullname;
updateProfileFactory.init($scope.dataProfile).then(
function (data) {
if (data.status == "success") {
// code
}
}
)
}
問題出在哪兒?
為什么不使用Angular的$http
庫來做AJAX呢? 事實證明,混合使用jQuery和Angular會產生不可預測的結果。
我可以看到您已經在應用程序中使用了Angular。 因此,確實沒有理由不應該完全放棄Angular。 $http
服務提供jQuery的$.ajax
提供的所有功能以及更多功能。
當您在原始Javascript或jQuery函數中使用Angular函數時,Angular不知道它們被調用了,因為它們不在其范圍內。 在這里您可以使用Angular的$apply
,但這並不理想,而且很hacky。
在這里,您的問題可能是$.ajax
的回調函數不知道$localStorage
因為它沒有作為依賴項傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.