在stateChangeStart中调用Angular api

时间:2016-03-01 11:30:33

标签: angularjs angular-ui-router

我正在尝试将用户重定向到我从api调用得到的某种状态:

$scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
             event.preventDefault();
             AppService
                 .currentState()
                 .success(function(data) {
                        $state.transitionTo(data.pageName, null, {notify:false});
                 });
         });

这会导致网址正确更改,但页面不会呈现。有什么想法吗?

我甚至尝试过没有像

这样的api调用
  $scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
                 event.preventDefault();
                 $state.go('app.home', null, {location: true, notify:false});
  });

2 个答案:

答案 0 :(得分:0)

使用$state.go()代替$state.transitionTo()

$state.go(data.pageName, null, {notify:false});

答案 1 :(得分:-2)

app.run(function ($rootScope, $location, Data) {

$rootScope.$on("$stateChangeStart", function (event, next, current) {
  $rootScope.authenticated = false;

  Data.get('auth/check_session').then(function (results) {

    if (results.uid) 
    {
      $rootScope.authenticated = true;
      $rootScope.uid = results.uid;

      var nextUrl = $location.path();

      if (nextUrl == '/signup' || nextUrl == '/signin' || nextUrl == '/forgot-password') {
        $location.path("/dashboard");
      }
    } 
    else 
    {
      //var nextUrl = next.originalPath;
      var nextUrl = $location.path();//next.$location.originalPath;

      if (nextUrl == '/signup' || nextUrl == 'app/signin') {
      } 
      else 
      {
        $location.path("/signin");
      }
    }
  });
});

});