Angularjs:隐藏包含动态参数的网址的导航栏
Angularjs: Hide navbar for urls containing dynamic parameters
我的angularjs应用程序中遇到了一个奇怪的情况。我有一个像下面这样的视图
索引.html
<body>
<nav ng-include="'/views/partials/nav_bar.html'" ng-if='location.path() !== "/" && location.path() !== "/signin" && location.path() !== "/register" && location.path() !== "/forgot_password" && location.path() !== "/update_password" && location.path() !== "{{page_id}}" '></nav>
<div data-ng-view>
</div>
</body>
你可以看到它不包含任何控制器初始化(因为所有其他具有控制器规范的页面都将呈现在data-ng-view
中)。但是我想隐藏特定页面的导航栏。我在上面的标签上使用ng-include
(非常丑陋的条件 - 我接受)。但是我有一个带有动态参数的页面,说'search/:id'
现在location.path()
无法工作.我的问题是我想隐藏一些静态#动态网址的导航栏。
我正在使用ng-token-auth
进行前端验证。
我会将条件移动到控制器中,然后您可以更简洁地确定导航是否应该可见。
这是一个工作示例。
angular.module('app', []).controller('BaseController', function($scope, $location) {
var excludeUrls = ["/signin", "/register", "/forgot_password", "/update_password"];
$scope.path = '/someUrl';
$scope.navbarIsVisible = function() {
var path = $scope.path; //this would be $location.path() normally, but for this example we are allowing it to be manually entered
if (path === '/') {
return false;
}
//anytime the url contains any variation of the exclude urls, hide the navbar
for (var i = 0; i < excludeUrls.length; i++) {
var excludeUrl = excludeUrls[i];
//if the path starts with the given url, hide the navbar
if (path.indexOf(excludeUrl) === 0) {
return false;
}
}
return true;
}
});
nav {
width: 100%;
height: 50px;
display: block;
background-color: grey;
color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="BaseController">
<nav ng-if='navbarIsVisible()'>I am the navbar and I am visible</nav>
Enter the url to see if the navbar should be visible:
<input type="text" ng-model="path" />
</div>
</div>
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 使用Express捕获参数
- 参数变量出现ngTable指令问题
- AngularJS:我可以跳过函数参数回调吗
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 要求未定义JS回调参数
- 我的jQuery插件参数没有正确启动,遇到了问题
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- angular 1.5应用程序中的导航栏
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- MVC 3页面导航和使用javascript传递参数
- Angularjs:隐藏包含动态参数的网址的导航栏
- Jquery 在 Firefox 中的自定义对象参数(动态导航加载动态内容)
- 使用$location导航并传递隐藏的参数
- 导航到浏览器中没有显示参数的页面
- 如何在Javascript中导航到具有复杂类型作为参数的MVC5控制器
- 如何在相同状态下的参数化url之间导航
- 带参数的Durandal子导航
- Ionic 2离子导航栏返回参数
- AngularJS通过URL ui-router简单导航到参数化状态