加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular – 相同的Url“/”,但根据用户的登录状态

发布时间:2020-12-17 07:44:49 所属栏目:安全 来源:网络整理
导读:我试图有两个不同的模板.一个是登录用户描述业务的登陆页面,另一个是用户登录时的其他仪表板页面. 我在this other stack上阅读了关于如何用UI路由器这样做.我想知道最好的做法,使用ngRoute做类似这样的事情? 下面设置的方式是“/ frontdesk”是仪表板,“/”
我试图有两个不同的模板.一个是登录用户描述业务的登陆页面,另一个是用户登录时的其他仪表板页面.

我在this other stack上阅读了关于如何用UI路由器这样做.我想知道最好的做法,使用ngRoute做类似这样的事情?

下面设置的方式是“/ frontdesk”是仪表板,“/”是注销用户的着陆页.但!我希望网址是相同的用户是否在仪表板上或已经注销并发送到着陆页!我不想要“/ frontdesk”,我需要“/”作为IndexController和FrontdeskController.

这是我的路由JS.

(function () {
      'use strict';

    angular
      .module('resonanceinn.routes')
      .config(config);

    config.$inject = ['$routeProvider'];

    function config($routeProvider) {
      $routeProvider
        .when('/',{ // This is the landing Page
            controller: 'IndexController',controllerAs: 'vm',templateUrl: '/static/javascripts/layout/index.html'
        })
        .when('/frontdesk',{ //This is logged in user that I want to become '/' as well
            controller: 'FrontdeskController',templateUrl: '/static/javascripts/frontdesk/frontdesk.html'
        })
        .when('/register',{
            controller: 'RegisterController',templateUrl: '/static/javascripts/authentication/register.html'
        })
        .when('/login',{
            controller: 'LoginController',templateUrl: '/static/javascripts/authentication/login.html '
        })
        .when('/:username',{
            controller: 'ProfileController',templateUrl: '/static/javascripts/profiles/profile.html'
        })
        .when('/:username/settings',{
            controller: 'ProfileSettingsController',templateUrl: '/static/javascripts/profiles/settings.html'
     }).otherwise('/');
   }
})();

解!

感谢大家的答案.

感谢@ThibaudL为更好的解决方案.

这是我最后做的事情

在Routes.js

.when('/',{
        controller: 'MainController',templateUrl: '/static/javascripts/layout/Main.html'

main.html中

<div ng-include="" src="'/static/javascripts/layout/index.html'" ng-if="auth"></div>
<div ng-include="" src="'/static/javascripts/frontdesk/frontdesk.html'" ng-if="!auth"></div>

MainController.js

(function () {
  'use strict';

  angular
    .module('resonanceinn.layout.controllers')
    .controller('StartController',StartController);

  StartController.$inject = ['$scope','$route','$routeParams','$location','Authentication'];

  function StartController($scope,$route,$routeParams,$location,Authentication) {

      $scope.auth = Authentication.isAuthenticated();
  }
})();

所以现在如果用户被认证,它只是将模板切换到用户信息板.
我添加了一个新的div与ng控制器到每个模板,使每个模板有其单独的控制器.

我会做的是:

main.html中

<div ng-include="" src="'/static/javascripts/layout/index.html'" ng-if="auth"></div>

MainController.js

(function () {
  'use strict';

  angular
    .module('App.layout.controllers')
    .controller('MainController',MainController);

  MainController.$inject = ['$scope','Authentication'];

  function MainController($scope,Authentication) { 

      $scope.auth = Authentication; 
   });
  }
})();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读