为什么在我点击屏幕之前 $location.path 不工作

Why is $location.path not working until I click on the screen

我有以下代码,旨在在成功验证后重定向到“./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 行,但我的问题是在我单击页面上的某些内容(不需要是按钮)之前页面没有任何反应。这是正常行为吗?

您正在从 Angular 世界之外修改 $location 路径(即自定义事件 authenticateUser 的 onSuccess 事件)。这就是为什么更改没有立即反映在 URL.

中的原因

从外部世界更改 angular 绑定不会直接在页面上更新当前更改,除非它通过摘要循环运行。为了使这些更改发生,您必须手动调用摘要循环来告诉 Angular digest 系统应用程序上下文中已经发生了一些更改。由于您必须启动摘要循环才能使它们同步。对于启动摘要循环,您可以使用 $timeout(最优选)/$scope.$apply 函数。

//inject `$timeout` in controller function
$timeout(function(){
   $location.path('/mainlandingpage'); 
})
cognitoUser.authenticateUser('authenticationDetails', function( $location) {
$location.path('/mainlandingpage');
       //your code here.......
});

您需要在函数中声明 $location,如图所示 above.try this