0

$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) {

あとは、フラッシュ メッセージを機能させて、「サインインする必要があります」と言うだけです。

4

0 に答える 0