角UI路由器:不同的国家有相同的URL? [英] Angular UI Router: Different states with same URL?

查看:110
本文介绍了角UI路由器:不同的国家有相同的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的登陆页面有两种状态:首页公开家庭已登录。现在我想展示在同一个URL两种状态,而是让控制器和模板取决于用户会话(用户登录或没有?)。

The landing page of my app has two states: home-public, home-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.

修改

作为承诺,工作普拉克

这篇关于角UI路由器:不同的国家有相同的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆