【问题标题】:How to put active class to specific id/footer of a html page using angular js?如何使用 Angular js 将活动类放到 html 页面的特定 ID/页脚?
【发布时间】: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


【解决方案1】:

restrict: 'A' 更改为restrict: 'E'

检查一次你写 ng-app 的地方,页脚是否包含在 ng-app 中?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多