【问题标题】:How to redirect with $state.go如何使用 $state.go 重定向
【发布时间】:2014-10-01 02:07:07
【问题描述】:

我试图在第三方登录后将我的用户重定向到仪表板。但是当成功回调被触发时,应用程序仍然在登录页面上,什么也没发生。如果我刷新浏览器,我的拦截器捕获已经登录并更改为仪表板...我的登录控制器如下所示:

ThirdParty.login(function(result){
callbackSUCCESS(result);
},function(){});


function callbackSUCCESS(result){
          AuthenticationService.login(result).then(
                            callbackServerSUCCESS(), function(reject) {
                                callbackServerERROR(reject);
                            });

    }
function callbackServerSUCCESS() {

                $scope.$apply(function() {
                    $state.go('dashboard');
                });
            }

我在 app.js 中的路线

$stateProvider
    .state('dashboard', {
        url: '/dashboard',
        views: {
            '': {
                templateUrl: 'views/dashboard/dashboard.html',
                controller: 'DashboardCtrl'
            }
        }
    });

我的标题控制器

.controller('HeaderCtrl', ['$scope', 'AuthenticationService', '$state',
        function($scope, AuthenticationService, $state) {

            $scope.logout = function() {
                AuthenticationService.logout().then(callbackServer(), callbackServer());
            };

            function callbackServer() {
                $state.go('login');
            }
        }
    ]);

身份验证控制器 Angular 工厂

 var headersConfig = {   
     'Cache-Control': 'no-cache',
     'Pragma': 'no-cache'
 };

 return {
     login: function(credentials) {
         var deferred = $q.defer();

         $http.post('/api/users/sign_in', sanitizeCredentials(credentials), {
             headers: headersConfig,
             timeout: deferred.promise
         }).then(function(result) {
             if (result.status === 200) {
                 UserSessionService.cacheSession(result.data);
                 deferred.resolve();
             } else {
                 deferred.reject();
             }
         }, function(reject) {
             UserSessionService.clean();
             deferred.reject(reject);
         });

         $timeout(function() {
             deferred.resolve();
         }, 15000);

         return deferred.promise;
     }
 };

【问题讨论】:

  • 我的第三方例如。 github。通量:登录页面->git auth->我的服务器登录->仪表板
  • 如果您在地址栏中输入 url,您会看到您的仪表板页面吗?
  • 不,当我重新加载页面时,然后更改为localhost:9000/dashboard
  • 我不认为你想要 then(callbackServer(), callbackServer()) 中的 ()

标签: angularjs angular-ui-router


【解决方案1】:

我不记得$state.go 的确切语义,但通常您需要在响应Angular 不知道的事件时以某种方式使用$scope.$apply,以确保发生摘要循环。 IE。你可以试试:

ThirdParty.login(function(result) {
    $scope.$apply(function() {
        $state.go('dashboard');
    });
}, function() {});

您需要从某个地方获取对范围的引用,但在 Angular 应用程序中应该不难找到。

【讨论】:

  • 我得到了一个范围的引用,但没有发生。控制台抛出:$apply 已经在进行中
  • 您的意思是您尝试了这段代码但没有成功?控制台有错误吗?
  • 登录什么也没发生,所以我重新加载页面以登录,然后我尝试注销和控制台抛出:错误:[$rootScope:inprog] $apply 已经在进行中errors.angularjs.org/1.2.25/$rootScope/inprog?p0=%24apply 在 VALIDITY_STATE_PROPERTY (@ 987654322@) 在 beginPhase (localhost:9000/bower_components/angular/angular.js:13009:15)
  • 我不知道你的注销逻辑中有什么,所以很难说是什么原因造成的。
  • 我编辑了问题以添加注销,但在登录中仍然没有工作。第三个依赖可以阻塞流吗?
【解决方案2】:

中去掉()
then(callbackServer(), callbackServer())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多