$ state仅适用于角度应用

时间:2016-04-07 16:36:01

标签: angularjs ionic-framework angular-ui-router angular-ui-router-extras

我已经让$ state工作在我的应用程序的一部分而不是另一部分。我第一次这样称呼它是这样的:$state.go('main');但我把它称为$state.go('signIn');,并且不能让它在那里工作。我希望此代码执行的操作是,如果用户未经$auth.validator验证,我希望它重定向到登录页面,并显示“您需要登录”的消息。我该怎么做呢?这是我的代码:

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ng-token-auth'])

.run(function($ionicPlatform, $location, $rootScope, $state) {

    //I added in order to have redirect upon succesful login
    $rootScope.$on('auth:login-success', function() {
      console.log('success event triggered yo main');
      // $location.path('main');
      $state.go('main');
    });


})

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

  .state('main', {
    url: '/main',
    templateUrl: 'templates/main.html',
    resolve: {
      auth: ['$auth', function($auth) {
        console.log($auth.validateUser());
        return $auth.validateUser().catch(function(res) {
          console.log('in the validate user block');
          $state.go('signIn');
        });
      }]
    }
  })

  .state('home', {
    url: '/home',
    templateUrl: 'templates/home.html'
  })

  .state('signUp', {
    url: '/signup',
    templateUrl: 'templates/signup.html',
    controller: 'UserCtrl'
  })

  .state('signIn', {
    url: '/sign_in',
    templateUrl: 'templates/new_user_session.html',
    controller: 'UserCtrl'
  })

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/home');

});

我得到了以下错误:

ionic.bundle.js:25642 ReferenceError: $state is not defined
    at app.js:57
    at processQueue (ionic.bundle.js:27879)
    at ionic.bundle.js:27895
    at Scope.$eval (ionic.bundle.js:29158)
    at Scope.$digest (ionic.bundle.js:28969)
    at Scope.$apply (ionic.bundle.js:29263)
    at bootstrapApply (ionic.bundle.js:14945)
    at Object.invoke (ionic.bundle.js:17762)
    at doBootstrap (ionic.bundle.js:14943)
    at bootstrap (ionic.bundle.js:14963)

更新:

我已经找到了问题的一半。我把$ state作为一个依赖项来获得工作:

auth: ['$auth', '$state', function($auth, $state) {

现在我只需要让flash消息工作并说“你需要登录”。

0 个答案:

没有答案