【发布时间】:2018-12-17 16:04:40
【问题描述】:
我正在为活动类使用以下指令,但这不会应用于页面上的特定 id
.directive('activeLink', ['$location', function (location) {
return {
restrict: 'A',
link: function(scope, element, attrs, controller) {
var active = attrs.activeLink;
var path = attrs.href;
path = path.substring(1); //hack because path does not return including hashbang
scope.location = location;
scope.$watch('location.path()', function (newPath) {
if (path === newPath) {
element.addClass(active);
} else {
element.removeClass(active);
}
});
}
};
}])
以下是我的导航栏主页,关于和产品是应用活动类的不同页面。但是“联系方式”是主页上的页脚,没有应用活动类。我怎样才能实现它?
<ul class="nav navbar-nav">
<li><a active-link="active" href="#/" target="_self">Home</a></li>
<li><a active-link="active" href="#/about" target="_self">About</a></li>
<li><a active-link="active" href="#/products" target="_self">Products</a></li>
**<li><a active-link="active" ng-click="scrollTo('contact')" href="#/#contact" target="_self">Contact Us</a></li>**
</ul>
【问题讨论】:
-
您是否还需要在您的主页上定义
active类? (在主页css文件内) -
你在说这个?? ---> .active{color:blue;}
-
是的。你确定它出现在你的主页上吗?
-
是的,现在..
-
请检查
newPath值(使用console.log、调试器等)。我怀疑在处理锚定时,您的path.substring(1)技巧不起作用(单击联系人时路径应为/#contact)
标签: javascript html angularjs css