导航栏中的下拉切换问题,使用ng-bootstrap(4)angular 2(cli)

时间:2016-12-25 14:39:22

标签: twitter-bootstrap angular drop-down-menu navbar ng-bootstrap

我按照here描述的说明操作,css工作正常。对于下拉切换,我使用了此代码

<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" id="supportedContentDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
      <div class="dropdown-menu" aria-labelledby="supportedContentDropdown">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
    </li>
  </ul>
  <form class="form-inline float-xs-right">
    <input class="form-control" type="text" placeholder="Search">
    <button class="btn btn-outline-success" type="submit">Search</button>
  </form>
</nav>

但是下拉切换功能不起作用,尽管我为Root添加了NgbModule。请知道我是否必须在我的组件提供程序数组或其他任何内容中包含其他内容。 提前致谢

3 个答案:

答案 0 :(得分:4)

我得到了这样的工作:

<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item dropdown">
      <div ngbDropdown class="d-inline-block">
        <button class="btn btn-outline-primary" id="dropdownMenu1" ngbDropdownToggle>Toggle dropdown</button>
        <div ngbDropdownMenu class="dropdown-menu" aria-labelledby="dropdownMenu1">
          <button class="dropdown-item">Action - 1</button>
          <button class="dropdown-item">Another Action</button>
          <button class="dropdown-item">Something else is here</button>
        </div>
      </div>
    </li>
  </ul>
  <form class="form-inline float-xs-right">
    <input class="form-control" type="text" placeholder="Search">
    <button class="btn btn-outline-success" type="submit">Search</button>
  </form>
</nav>

如上文评论(link

所述

答案 1 :(得分:3)

我最终做了这样的事情:

在html模板中我有这段代码:

 <nav id="mainNavbar" class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse"
           data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
           aria-expanded="false" aria-label="Toggle navigation
           (click)="toggleCollapsed()">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div id="navbarSupportedContent" [ngClass]="{'collapse': collapsed, 'navbar-collapse': true}">
......
</nav>

在我有这个

的组件中
 export class NavComponent {
      collapsed = true;
      toggleCollapsed(): void {
        this.collapsed = !this.collapsed;
      }
 }

答案 2 :(得分:3)

将你淹没的html部分修改为:

<li class="nav-item dropdown">
  <div ngbDropdown class="d-inline-block">
    <button class="btn btn-outline-primary" id="dropdownMenu1" 
      **ngbDropdownToggle**>use Attrib ngbDropdownToggle</button>

    <!-- use  ngbDropdownMenu -->
    <div **ngbDropdownMenu** class="dropdown-menu" aria-labelledby="dropdownMenu1">
      <button class="dropdown-item">Action - 1</button>
      <button class="dropdown-item">Another Action</button>
      <button class="dropdown-item">Something else is here</button>
    </div>
  </div>
</li>