angular - 为事件路由器链接的父级设置一个类,Angular 2+

标签 angular css

我有一个带下拉菜单的 bootstrap 4 导航栏,下拉菜单包含路线,我可以根据“routerLinkActive”使它们显示为事件状态,但我还想设置“下拉切换”或“导航元素”的样式当它的一个 child 是事件路线时,它是事件的。

我该怎么做呢?

这里是一小段代码,为了便于阅读,我试着清理了一下

<li *ngFor="let menuItem of MenuItems; index as i" [id]="i" class="nav-item dropdown" ngbDropdown>

  <a class="nav-link dropdown-toggle" ngbDropdownToggle>
    {{menuItem.title}}
  </a>

  <!-- dropdown menu -->
  <div *ngIf="menuItem.submenu.length > 0" class="dropdown-menu" ngbDropdownMenu aria-labelledby="i">
    <div *ngFor="let menuSubItem of menuItem.submenu">
        <a  [routerLink]="menuSubItem.path"
            [routerLinkActive]="['active-sub']" <== ** this part works and sets the class, now i need the top nav-link to be active too
            [routerLinkActiveOptions]="{exact: true}" 
            class="dropdown-item">
            {{menuSubItem.title}}
        </a>
    </div>
  </div>

</li>

最佳答案

您可以像这样使用 routerLinkActive 模板引用:

<li [ngClass]="{'active-class': rlRef.isActive}">
  <a [routerLink]="['/your-route']" routerLinkActive="active" #rlRef="routerLinkActive">Fancy URL</a>
<li>

关于angular - 为事件路由器链接的父级设置一个类,Angular 2+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54518647/

相关文章:

css - 如何在 CSS 背景中正确制作 SVG Sprite 图 block ?

html - 使用 flexbox 创建一个固定的头部

html - 如何在任何屏幕分辨率下将网页内容设置为 100%?

javascript - 使用 JSON 根元素可观察到的 Angular2

javascript - 找不到模块 'pouchdb' ,适用于 Mac,但不适用于 Windows

angular - 在 npm 安装上有 jasmine 错误,angular4

javascript - !important 并显示 :none and . height() 怪异

css - 链接到 div 的 ID 从 div 顶部以下开始?

string - Angular:从路径加载 SVG 并将其字符串化

javascript - 如何将 NgbDatePicker 格式从 YYYY-MM-DD 更改为 MM-DD-YYYY angular 4?