javascript - Angular View ,如果未经身份验证,则显示单个 View

标签 javascript angularjs

我正在使用wrapbootstrap 中的SmartAdmin 主题。我添加了大量自己的 Angular 代码 ~5k loc。

到目前为止,我已经使用主题的方法来查看渲染,但是,现在它成了一个问题,我需要一种方法来解决登录页面的问题。

项目的文件结构

project/
  assets/
    js/
      app/
        feature1/
          controller.js
          directive.js
          filter.js
  views/
    feature1/
      template.html
    feature2/
  index.html

当用户第一次进入项目根目录时,index.html已送达。里面index.html有各种ng-includes用于页眉、侧面导航和页脚。

我在每个请求之间使用基于 JWT 的身份验证和授权,如果用户未经过身份验证或超时,它将重定向到 /login通过$location.path('/login');

问题是它仍然显示页眉、侧面导航和页脚(来自 project/index.html )。无论如何,有没有办法解决这个问题,并让它只使用 index.html如果经过身份验证?

正如您所料,我不希望用户在未经身份验证时看到页眉/侧导航/页脚。不管templateUrl我在路由器中指定 index.html服务完毕,templateUrl在其内部使用。我需要这种情况不要只发生在特定路线上。

编辑:

我使用的模板是通过 <div ng-view></div> 插入的在 index.html .

index.html 的模拟布局:

<html ng-app="app">
  <head>
  </head>
  <body ng-controller="PageCtrl">
    <header ng-include="'templates/header.html'"></header>
    <aside ng-controller="'templates/left-panel.html'"></aside>
    <div ng-view></div>
    <footer ng-include="'templates/footer.html'"></footer>
  </body>
</html>

ng-view 的内部每个模板都有一个 Controller 。

最佳答案

在处理应用程序安全问题时,使用 ng-if 更加安全可靠。 ng-show 和 ng-hide 可以工作,但这只能设置/重置经过身份验证的标题和菜单的可见性。

因此,通过在脚本部分进行身份验证时设置类似 $scope.authenticated = true; 的范围变量,并在模板中使用该变量,如 ng-if="authenticated “

关于javascript - Angular View ,如果未经身份验证,则显示单个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26637512/

相关文章:

javascript - 如何使用 Backbone 生态模板中的自定义模型功能?

javascript - 如何使用 React 正确更新数组中的状态?

javascript - 从 Google Apps 脚本中的日期中删除时间戳

javascript - Angular js-值更改时突出显示dom

javascript - Facebook 登录设置 : Where is the "Enable Client Access Token Flow" switch?

javascript - 选项 url 路径 UI-Router

javascript - 取消 $timeout 不起作用

javascript - 模型不是 AngularJS 中输入的日期对象

angularjs - Angular:req.handle.writev 不是函数,运行 Angular 旧应用程序时出错

java - Rhino 中的 JS 属性赋值抛出异常有意义吗?