码迷,mamicode.com
首页 > Web开发 > 详细

angular 的ui.router 定义不同的state 对应相同的url

时间:2017-12-06 23:49:22      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:show   UI   load   control   ase   iat   dep   tty   overflow   

Angular UI Router: Different states with same URL?

 

The landing page of my app has two states: home-publichome-logged-in. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user logged in or not?).

Is there a way to achieve this?

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You could have a base state that controls which state to load, and you could simply have the child stated of that base state not have urls:

.state(‘home‘, {
  url: "/home",
  templateUrl: "....",
  controller: function($scope,$state,authSvc) {
     if(authSvc.userIsLoggedIn()){
          $state.go(‘home.loggedin‘)
     }else{
          $state.go(‘home.public‘)
     }
  }
})


.state(‘home.public‘, {
  url: "",
  templateUrl: "....",
  controller: function($scope) {
     ...........
  }
})

.state(‘home.loggedin‘, {
  url: "",
  templateUrl: "....",
  controller: function($scope) {
     ...........
  }
})

Now in the controller of your base state (home) you can check if the user is logged in or not, and use $state.go() to load an appropriate state.

angular 的ui.router 定义不同的state 对应相同的url

标签:show   UI   load   control   ase   iat   dep   tty   overflow   

原文地址:http://www.cnblogs.com/oxspirt/p/7995043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!