【问题标题】:$location.path('/content'); doesnt work, regardless of what I do. Why?$location.path('/content');不起作用,不管我做什么。为什么?
【发布时间】:2014-01-16 23:26:08
【问题描述】:

这是一个后续问题:When clicking on login with facebook/persona button with angularFire v0.5.0 ($firebaseAuth) user gets redirected immediately to '/'.Why?

我遇到了另一个问题,因为这现在不起作用:

$rootScope.$on('$firebaseAuth:login', function(){
                    $location.path('/content');

因为在用户完成身份验证后,他们应该被重定向到 /content(而不是卡在登录页面,登录按钮由于 ng-hide 而消失(见上文)),但我尝试 $location 并不重要。 path('/content') 不起作用。

我试过了:

waitForAuth.then(function(){
                  console.log('test');
                  $location.path('/content');
              })

但是 console.log 在用户通过身份验证之前以控制台方式打印出“test”,所以 waitForAuth 似乎也不起作用:/。不知何故,waitForAuth 会触发 console.log,但不会触发 $location.path... 奇怪。

我什至尝试过这样做(使用 waitForAuth 和 $rootScope.$on:

waitForAuth.then(function(){
                      console.log('test');
                      $location.path('/content');
                      $scope.$apply();
                  })

但我只是得到一个错误,角度 $digest 已经在进行中。

但是,如果我这样做:

app.controller('controller',['$scope','$firebaseAuth','$location','$rootScope',function($scope, $firebaseAuth,$location,$rootScope) {

            $scope.$on('$locationChangeStart', function(event) {
                event.preventDefault();
              });


            var ref = new Firebase('https://mybase.firebaseio.com/');
            $scope.auth = $firebaseAuth(ref);
            $rootScope.$on('$firebaseAuth:login', function(){
                console.log("root scope");
                $scope.$apply(function(){
                    console.log("scope");
                    $location.path('/content');
                });

            });

        }]);

控制台两次注销“root scope”和“scope”,时机恰到好处:只有在用户完成身份验证后,但 $location.path() 仍然不起作用并且用户卡在登录页面/视图。

这也不行:

$rootScope.$on('$firebaseAuth:login', function(){
                    console.log("root scope");
                    $scope.$apply(function(){
                        console.log("scope");
                        $location.path('/content');
                        $location.replace();

                    });

我需要一些帮助:)

【问题讨论】:

    标签: angularjs angularjs-scope firebase angularfire


    【解决方案1】:

    我已通过从以下位置删除 authRequired: true 解决了我的问题:

    $routeProvider.when('/content',{
                authRequired: true, [removed]
                templateUrl: 'content.html',
                controller: 'MyController'
            });
    

    并像这样更改控制器:

    app.controller('controller',['$scope','$firebaseAuth','$location','$rootScope',function($scope, $firebaseAuth,$location,$rootScope) {
    
    
    
                    $scope.$on('$locationChangeStart', function(event,next,current) {
                    if (next == 'http://angular.ngrok.com/angularfire/'){event.preventDefault();}
                });
    
                var ref = new Firebase('https://mybase.firebaseio.com/');
                $scope.auth = $firebaseAuth(ref);
                $rootScope.$on('$firebaseAuth:login',function(){
                    $location.path('/content');
                });
    
            }]);
    

    变化:

    event.preventDefault(); 必须是具体的,否则它会阻止所有$location.path();

    waitForAuth 不起作用,因为它并没有真正等待 Auth,它会立即触发,因此我必须使用 $rootScope.$on


    我还在 angularFire Github 上打开了一个问题:

    https://github.com/firebase/angularFire/issues/197

    现在我必须找到另一种方法来防止未经授权的访问:(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2016-05-27
      • 2014-07-27
      • 2019-07-31
      • 1970-01-01
      • 2017-02-22
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多