Angularjs 为什么在我单击屏幕之前$location.path不起作用

Angularjs 为什么在我单击屏幕之前$location.path不起作用,angularjs,amazon-cognito,Angularjs,Amazon Cognito,我有以下代码,目的是在成功验证后重定向到“/mainlandingpage”。以下是来自控制器的代码: cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { // pass token to use for getting credentials later sessionStorage.Token = res

我有以下代码,目的是在成功验证后重定向到“/mainlandingpage”。以下是来自控制器的代码:

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            // pass token to use for getting credentials later
            sessionStorage.Token = result.getIdToken().getJwtToken();
            $scope.messageText = globalData.LoggingIn;
            $location.path('/mainlandingpage');
            console.log(' after location');

        },

        onFailure: function(err) {
            alert(err);
        }
Firebug的控制台显示以下内容:

POST https://cognito-idp.ap-northeast-1.amazonaws.com/200 OK 704ms 
POST https://cognito-idp.ap-northeast-1.amazonaws.com/200 OK 401ms 
after location 

因此,我知道代码通过了$location.path行,但我的问题是,在单击页面上的某个内容(不需要是按钮)之前,页面不会发生任何变化。这是正常的行为吗

您正在修改来自外部角度世界的
$location
路径(即自定义事件验证器的
onSuccess
事件)。这就是为什么更改不会立即反映在URL中

从外部世界更改角度绑定不会直接更新页面上的当前更改,除非它通过摘要循环运行。要进行这些更改,您必须手动调用摘要周期,告诉
Angular digest
系统在应用程序上下文中发生了一些更改。因为你们必须启动消化循环,使它们同步。对于启动摘要循环,您可以使用
$timeout
(首选)/
$scope.$apply
功能

//inject `$timeout` in controller function
$timeout(function(){
   $location.path('/mainlandingpage'); 
})

您需要在函数中声明$location,如上所示。尝试此

依赖项可以注入到
控制器
级别函数中,您所拥有的不正确。@leoc很高兴知道,谢谢:)\
cognitoUser.authenticateUser('authenticationDetails', function( $location) {
$location.path('/mainlandingpage');
       //your code here.......
});