收起时显示不同的导航栏项目(ngx-bootstrap)

时间:2019-02-04 01:49:56

标签: angular bootstrap-4 ngx-bootstrap

使用ngx-bootstrap,是否可以根据导航栏是否折叠来显示不同的导航栏引导项?

我试图通过为折叠和!collapsed设置div来做到这一点。不幸的是,两者都显示!崩溃的时间。

StackBlitz:https://stackblitz.com/edit/angular-startup-proj-ddztm4

<nav class="navbar navbar-expand-sm navbar-light bg-light">
  <a class="navbar-brand"
     href="#">Navbar</a>
  <button class="navbar-toggler"
          type="button"
          data-toggle="collapse"
          data-target=".navbarSupportedContent"
          aria-controls="navbarSupportedContent"
          aria-label="Toggle navigation"
          [attr.aria-expanded]="!isCollapsed"
          (click)="isCollapsed = !isCollapsed">
      <span class="navbar-toggler-icon"></span>
  </button>

  <!-- show only when not collapsed -->
  <div class="collapse navbar-collapse navbarSupportedContent"
       [ngClass]="{'hide': isCollapsed}">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link"
           href="#">Link One - full screen</a>
      </li>
      <li class="nav-item">
        <a class="nav-link"
           href="#">Link Two - full screen</a>
      </li>
    </ul>
  </div>

  <!-- show only when collapsed -->
  <div class="collapse navbar-collapse navbarSupportedContent"
  [ngClass]="{'show': !isCollapsed}">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link"
            href="#">Link Two - collapsed</a>
      </li>
      <li class="nav-item">
        <a class="nav-link text-success"
            href="#">Link Three - collapsed</a>
      </li>
    </ul>
  </div>
</nav>

TS:

 isCollapsed = true;

1 个答案:

答案 0 :(得分:1)

如果您将[style.display]="isCollapsed?'inherit':'none'"添加到<ul class="navbar-nav mr-auto"中是可能的:

<!-- show only when not collapsed -->
<div class="collapse navbar-collapse navbarSupportedContent"
[ngClass]="{'hide': isCollapsed}">
  <ul class="navbar-nav mr-auto">
  <!-- rest of code .... -->
</div>
<!-- show only when collapsed -->
<div class="collapse navbar-collapse navbarSupportedContent"
[ngClass]="{'show': !isCollapsed}">
  <ul class="navbar-nav mr-auto" [style.display]="isCollapsed?'inherit':'none'">
  <!-- rest of code .... -->
</div>

更新:

要使其具有响应能力,您可以使用@media queries(例如w3school中的示例):

@media (max-width: 576px){
   ul.collapse{
      display:none !important;
   }
}

@media (min-width: 576px){
   ul.full{
      display:none !important;
   }
}

css中,然后添加以下ul类:

<!-- show only when not collapsed -->
`<ul class="navbar-nav mr-auto collapse">`

<!-- show only when collapsed -->
<ul class="navbar-nav mr-auto full" [style.display]="isCollapsed?'inherit':'none'">

Stackblitz Fork

上的示例