简体   繁体   English

重定向到angularjs中的另一个视图

[英]Redirect to another view in angularjs

When user logged in i am setting a token in localstorage if token is exists it will go the blogs page otherwise launch login page. 当用户登录时,如果存在令牌,则在localstorage中设置一个令牌,它将进入博客页面,否则将启动登录页面。

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

    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })
  .state('app.login', {
      url: '/login',
      views: {
        'menuContent': {
          templateUrl: 'templates/login.html',
          controller:'LoginCtrl'
        }
      }
    })
.state('app.blog', {
    url: '/blog',
    views: {
      'menuContent': {
        templateUrl: 'templates/blog.html',
        controller: 'PlaylistCtrl'
      }
    }
  })

  //if token null got login
     if(window.localStorage.getItem('usertoken')==null){
      $urlRouterProvider.otherwise('/app/login');
     }else{
       //go to blog
       alert('logged');
       $urlRouterProvider.otherwise('/app/blog');
     }

});

I will get alert('logged') but it not go the blog view intstead it go to login view 我将收到alert('logged')但不会进入blog视图,而是进入login视图

You can make an intermediate view. 您可以进行中间视图。 Imagine dashboard.html 想象一下dashboard.html

<ion-view >
  <ion-content>
  </ion-content>
</ion-view>

Make your otherwise go there 让你否则去那里

$urlRouterProvider.otherwise('/app/dashboard');

And in this controller do the comprobation 并在此控制器中进行称赞

.controller('DashboardCtrl', function($rootScope, $state) {

if(window.localStorage.getItem('usertoken')==null){
      $state.go('/app/login');
     }else{
       //go to blog
       alert('logged');
       $state.go('/app/blog');
     }
});

尝试这个

$urlRouterProvider.otherwise(window.localStorage.getItem('usertoken')==null ? '/app/login' : '/app/blog');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM