Angular 2 Responsive Header - Expand/toggle button not working in mobile view

左心房为你撑大大i 提交于 2019-12-13 00:15:49

问题


The toggle button appears in mobile view but it's not clickable. In angular appDropdown is used to define a drop down button. Is there a similar approach to fix the toggle button?

Unfortunately I cannot show images here but there's another problem with "Administration" drop down button. It changes colour to grey when I hover over the internal <li> (such as "Registration"...).

Adding href= "#" removes the unwanted color change but this refreshes the whole page when its clicked.

Here's the HTML:

<nav class="navbar navbar-webmaster">
<div class="container">
    <div class="navbar-header">
        <button type="button" appDropdown class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" [routerLink]="['/']">Title</a>
    </div>

    <div class="collapse navbar-collapse" id="navbar">
        <ul class="nav navbar-nav navbar-right">
            <li class="active"><a href="#">Sublime<span class="sr-only"></span></a></li>
          <li class="dropdown" appDropdown *ngIf="userService.isAdmin">
      <a class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false">Administration <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a [routerLink]="['/register']">Registration</a></li>
        <li><a>Other Admin  Stuff</a></li>
        <li role="separator" class="divider"></li>
        <li><a>Another Admin Stuff</a></li>
      </ul>
    </li>
        </ul>
        <form class="navbar-form navbar-right search-form" role="search">
            <input type="text" class="form-control" placeholder="Search" />
        </form>
    </div>

</div>


The CSS:

nav.navbar-webmaster {
  background: #00547E;
}

nav.navbar-webmaster a {
  color: #fff;
}

nav.navbar-brand :hover {
  color: #337ab7;
}

nav.navbar-webmaster ul.navbar-nav a:hover,
nav.navbar-webmaster ul.navbar-nav a:visited,
nav.navbar-webmaster ul.navbar-nav a:focus,
nav.navbar-webmaster ul.navbar-nav a:active {
  background: #0d3a51;
}

nav.navbar-webmaster ul.navbar-nav a:hover {
  border-color: #337ab7;
}

nav.navbar-webmaster li.divider {
  background: #ccc;
}

nav.navbar-webmaster button.navbar-toggle {
  background: #999;
  border-radius: 2px;
}

nav.navbar-webmaster button.navbar-toggle:hover {
  background: #999;
}

nav.navbar-webmaster button.navbar-toggle>span.icon-bar {
  background: #fff;
}

nav.navbar-webmaster ul.dropdown-menu {
  border: 0;
  background: #fff;
}

nav.navbar-webmaster ul.dropdown-menu>li>a {
  color: #444;
}

nav.navbar-webmaster ul.dropdown-menu>li>a:hover {
  background: #00547E;
  color: #fff;
}


Any help is greatly appreciated.


回答1:


Implemented by using ng-bootstrap. Update html as below

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Logo</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar" [ngbCollapse]="isCollapsed">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Projects</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>
    </div>
  </div>
</nav>

Declare below variable in component class

public isCollapsed = false;

For live action, visit my Plunker



来源:https://stackoverflow.com/questions/41749857/angular-2-responsive-header-expand-toggle-button-not-working-in-mobile-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!