2016-09-30 62 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,所以你回答没有帮助。 –