问题描述
我想要的←回到首页链接出现在nagivation我下面的角应用程序的每一页,除了主页上。所以我想有条件地添加链接,并使用其隐藏 NG-隐藏
如果URL已经是应用程序的主页(视图)上。
I'd like a ← Back to home link to appear below the nagivation on every page of my Angular app except the home page. So I'd like to conditionally add the link and hide it using ng-hide
if the url is already on the home page (view) of the app.
我采用了棱角分明的 $位置
服务没有成功尝试
I've tried using angular's $location
service with no success
<p ng-hide="location.hash == '#/'" class="container"><a href="#topics">← Back to Home</a></p>
我试过以下变化:
I've tried the following variations:
ng-hide="location.hash == '#/' " //console.log shows true
ng-hide="location.hash === '#/' " //console.log shows true
ng-hide="location.hash == '' " //console.log shows false
我很困惑,因为如果我登录的location.hash =='#/'的值
当主页我得到真
,所以 NG-隐藏
应该工作。
I'm puzzled because if I log the value of location.hash == '#/'
when on the home page i get true
, so ng-hide
should work.
基本上我想在这里列出的第三个办法:
How我用角NG隐藏基于页/路由我目前在?上但它不工作。该网页上的其他两种方法似乎为我想要实现过于复杂。
Basically I'm trying the 3rd approach listed here:How do I use angular ng-hide based on the page/route that I am currently on? But it's not working. The other two approaches on that page seem overly complicated for what I'm trying to achieve.
我是什么失踪?
推荐答案
首先第一件事情,当你使用的location.hash或者你实际使用window.location的JavaScript对象location.url,你应该使用所提供的$位置服务通过角。
因此,在您的控制器我会创造:
First things first, when you use location.hash or location.url you are actually using the window.location javascript object, you should use the $location service provided by angular.So in your controller I would create:
$scope.currentPath = $location.path();
而在你的HTML:
And in your html:
<div ng-hide="currentPath === '/'"></div>
芹苴我会小心对#/和/,我只用HTML5的方式,所以我不知道$ location.path将返回什么,但你可以轻松地用的console.log检查($ location.path()),尽管我认为这只会返回/,因为这是对角的路径,应该不会在乎#。
Tho I would be carefull about the "#/" and "/", I only use the html5 mode so I'm not sure what the $location.path will return but you can easily check it with a console.log($location.path()) tho I think it will only return "/" because that's the path for angular, it shouldn't care about the #.
这篇关于我如何使用NG隐藏如果$ location.url ==基地(根)网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!