【问题标题】:Angularjs role-based routingAngularjs 基于角色的路由
【发布时间】:2016-06-12 03:39:58
【问题描述】:

我正在开发一个 Angular 应用程序。到目前为止,我有一个包含应用程序主模板的 layout.html 文件。 partials/ 中还有一些其他文件是通过此代码路由的:

angular.module('appRoutes', []).config(['$routeProvider',             '$locationProvider', '$httpProvider',
  function ($routeProvider, $locationProvider, $httpProvider) {
    $routeProvider
      .when('/', {
        templateUrl: '/partials/index',
        controller: 'MainController'
      })
      .when('/:category/:action?/:id?', {
        templateUrl: function (params) {
          var allowedParams = ['category', 'action', 'id'];
          var paramVals = [];
          for (var key in params) {
            if (allowedParams.indexOf(key) !== -1) {
              paramVals.push(params[key]);
            }
          }
          console.log(paramVals.join('/'));
          return '/partials/' + paramVals.join('/');
        }
      })
      .otherwise({
        redirectTo: '/'
      });
]);

到目前为止,它运行良好。不过会比较复杂。我想展示基于角色的视图。每个视图之间的主要区别在于侧边栏导航内容。举个例子:如果我输入 www.domain.com/admin-dashboard 或 www.domain.com/user-dashboard 类似的视图将被呈现,但是每个角色可用的选项和菜单将是不同的。我的第一次尝试是创建一个 admin-layout.html 和一个 user-layout.html。但是我不知道这是否是一个正确的解决方案,并且我在编写代码时遇到问题,因此它使用一个模板或另一个模板。

任何想法都会受到赞赏。

更新:

假设我有一个 layout.html

<html lang="en">
<head>
</head>
<body ng-app="todoApp" ng-controller="MainController" class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">


        <!-- ####### Layout 1: IF the user is logged in: (show header and sidebar depending on the role) -->
        <!-- Header: remains the same regardless the role -->
        <header class="main-header"></header>
        <!-- Left side column: changes according to the role -->
        <aside class="main-sidebar"></aside>
        <!-- Content -->
        <div class="content-wrapper">   
            <section ng-view class="content">
            </section>
        </div>
        <!-- ####### !Layout 1: end of Layout 1 -->


        <!-- ####### Layout 2: IF the user is not logged in: (show a simple login form in the middle) -->
        <!-- Content -->
        <div class="content-wrapper">   
            <section ng-view class="content">
            </section>
        </div>
        <!-- ####### !Layout 2: end of Layout 2 -->

        <!-- Footer: remains the same always -->
        <footer class="main-footer"></footer>
    </div>
</body>

我可以确定登录的用户角色,但是根据角色我想在侧边栏上显示不同的信息。这可以使用如下阿里建议的 data-ng-include 来完成。但是,如果 Id 想为登录页面呈现不同的模板作为示例(没有侧边栏或导航栏,只有带页脚的空白画布),或者如果我想呈现 3 列模板。这如何才能正确完成?在给定特定条件的情况下,如何指示 angular 使用不同的模板。再次感谢。

【问题讨论】:

  • 另一个例子:有一个 www.example.com/login 视图。我只需要一个可以呈现登录表单的纯白色模板。我不需要其余视图中可用的导航或页脚。

标签: angularjs routing


【解决方案1】:

你可以使用 data-ng-include

例如:

 <div class="mainContainer">
 <div data-ng-include="{{navBarType}}" >
 </div>

在您的控制器或指令中,您可以根据需要设置 navBarType,例如 navBarUser.html。

您还可以阅读有关 ngInclude 的更多信息 这里: https://docs.angularjs.org/api/ng/directive/ngInclude

【讨论】:

  • 谢谢阿里。这将根据特定条件渲染视图的一部分(在本例中为侧边栏)。但是,如果我想渲染一个完全不同的布局,这会起作用吗?我在问题中添加了一个更新来说明这个想法。
  • 实际上单独的布局更安全,更灵活,以后可以修改,但是你可以使用 data-ng-if 来显示目标 div。
  • 嗨,阿里。这正是我想要做的。让我们说管理员视图的一种布局,用户视图的第二种布局和登录页面的第三种布局。我相信我应该使用 UI 路由器 github.com/angular-ui/ui-router。但我不确定。只需查看文档即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2015-07-17
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多