AngularJS $location.path 循环回到根目录

AngularJS $location.path looping back to root

我找遍了所有地方,但 none 的潜在解决方案都奏效了。

登录页面应该在成功登录或注册后路由到选项卡页面。由于某种原因,它每次都指向根或 otherwise 状态。有没有人有什么建议?谢谢!

州:

.config(function($stateProvider, $urlRouterProvider) {

$stateProvider
.state('intro', {
url: '/intro',
templateUrl: 'templates/intro.html',
controller: 'IntroCtrl'
})
 .state('signin', {
  url: '/sign-in',
  templateUrl: 'templates/sign-in.html',
  controller: 'SignInCtrl'
})
.state('forgotpassword', {
  url: '/forgot-password',
  templateUrl: 'templates/forgot-password.html'
})
.state('tabs', {
  url: '/tab',
  abstract: true,
  templateUrl: 'templates/tabs.html'
})
.state('tabs.home', {
  url: '/home',
  views: {
    'home-tab': {
      templateUrl: 'templates/home.html',
      controller: 'HomeTabCtrl'
    }
  }
});


 $urlRouterProvider.otherwise('/intro');

})

登录时应该路由到选项卡页面的控制器:

  .controller('SignInCtrl', function($scope, $state, $firebaseAuth, $location) {

$scope.login = function(username, password) {
    var fbAuth = $firebaseAuth(fb);
    fbAuth.$authWithPassword({
        email: username,
        password: password
    }).then(function(authData) {
        $location.path('/tab')
    }).catch(function(error) {
        console.error("ERROR: " + error);
    });
}

$scope.register = function(username, password) {
    var fbAuth = $firebaseAuth(fb);
    fbAuth.$createUser({email: username, password: password}).then(function() {
        return fbAuth.$authWithPassword({
            email: username,
            password: password
        });
    }).then(function(authData) {
        $location.path("/tab");
    }).catch(function(error) {
        console.error("ERROR " + error);
    });
}
})

由于您使用的是 ui-路由器和基于状态的路由,只需调用 $state.go() 即可在状态之间进行 quick 导航。注意 - 您可能需要从 tabs 状态中删除 abstract: true 或过渡到 tabs.home。例如...

.then(function(authData) {
     // $location.path('/tab');
     $state.go('tabs')
})

有关详细信息,请参阅 documentation - $state.go(to [, toParams] [, options])

Convenience method for transitioning to a new state