AngularJS $ location.path不上解析工作承诺 [英] AngularJS $location.path doesn't work on Parse Promise
问题描述
我的解析/ AngularJS应用程序正在使用的承诺。
在登录功能是假设用户是 $ location.path 重定向,但我不得不调整它,使其根据本岗位工作:
My parse/AngularJS application is using Promises. The login function was suppose to redirect the user with $location.path, BUT I had to tweak it to make it work according to this post:
<一个href=\"http://stackoverflow.com/questions/19499323/location-path-doesnt-change-in-a-factory-with-angularjs\">$location.path在工厂与AngularJS
根据这一SO用户,您需要将调用替换为 $ location.path 与
According to this SO user, you need to replace the call to $location.path with
$rootScope.$apply( function(){$location.path('/somelocatin'); } );
请参阅回答这里:
<一href=\"http://stackoverflow.com/a/19738154/1743693\">http://stackoverflow.com/a/19738154/1743693
这解决了这个问题,但我已经明白:为什么没有本机调用的工作?以及如何做的调整解决这个问题?我怎么知道我不是搞砸了的AngularJS周期?或者是它的东西,是解析实现无极模式相关的更多信息?
This solved the issue, but I have to understand: WHY doesn't the native call work? and HOW does the tweak solve the issue? how do I know I'm not screwing up the AngularJS cycle? or is it something that is related more to Parse implementation of the Promise pattern?
这是登录code:
function login() {
vm.dataLoading = true;
var response = AuthenticationService.Login(vm.email, vm.password).then(
function (user) {
vm.dataLoading = false;
//$location.path('/'); -- <-- **doesn't work**
vm.user = Parse.User.current().getEmail();
$rootScope.$apply(function () { $location.path('/'); });
},
function () {
$rootScope.$apply(function () { $location.path('/login'); });
FlashService.Error(response.message);
vm.dataLoading = false;
});
};
和认证服务实现:
function Login(username, password, success, error) {
return Parse.User.logIn(username, password, {
success: function (user) {
return user;
},
error: function (user, error) {
return { user: user, error: error };
}
});
}
(原始登录源可以在这里找到:的http://jasonwatmore.com/post/2015/03/10/AngularJS-User-Registration-and-Login-Example.aspx)
推荐答案
由于 $位置
的紧紧绑以角消化周期,以及许多其他的角度内置服务,所有的 $ location.path
的做的是更新内部$$ path属性。它将被用于更新下一个摘要中的路径。
Because $location
is tightly tied to Angular digest cycles, as well as many other Angular built-in services, all that $location.path
does is updating internal $$path property. It will be used to update the path during the next digest.
而因为 $ HTTP
是依赖于消化为好,它触发时的承诺正在得到解决或拒绝摘要。一般来说没有必要担心手动触发摘要,$位置将更新下消化过程中的位置。在列举问题的州对方回答这个明确。
And because $http
is tied to digests as well, it triggers the digest when the promise is being resolved or rejected. Generally there's no need to worry to trigger the digest manually, $location will update the location during the next digest. The other answer in the cited question states this unambiguously.
由于承诺没有被提供内置的服务,但解析
,消化需要手动触发,这样角知道的地点是更新。考虑
Since the promise wasn't provided by built-in service but by Parse
, the digest needs to be triggered manually, this way Angular 'knows' that the location was updated. Consider
$location.path('/');
$rootScope.$apply();
语法,而是如果让您随心所欲了。
syntax instead if it pleases you more.
有可能建立一个观察者,当消化打在那一刻与位置路径会发生什么情况,调查
It is possible to set up a watcher to investigate when the digest hits and what happens with location path at that moment
$rootScope.$watch(function () {
console.log('digest! and the path is ' + $location.$$path);
});
这篇关于AngularJS $ location.path不上解析工作承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!