繁体   English   中英

Angular.js:隐藏导航栏查找包含动态参数的网址

[英]Angularjs: Hide navbar for urls containing dynamic parameters

我的angularjs应用程序出现了一个奇怪的情况。 我有下面这样的观点

index.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()无法工作。 我的问题是我想为某些static#dynamic url隐藏导航栏。

我正在使用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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM