ng-sidebar(Angular 2)实现问题

时间:2017-07-12 18:30:40

标签: javascript angular angular-directive

我需要使用ng-sidebar在标题中的点击按钮左侧显示可折叠的侧边栏菜单。

app.component.html

<div class="page-header custom-phead">
    <ng-sidebar-container>
     <ng-sidebar [(opened)]="_opened">
        <p>Sidebar contents</p>
      </ng-sidebar>

<div ng-sidebar-content>
<button (click)="_toggleSidebar()">Toggle</button>
</div>
</ng-sidebar-container>
<form class="heading">
<input type="text" name="search" [(ngModel)]="search"><input type="image" [src]="'../resource/searchicon.png'">
</form>
</div>

在app.component.ts中,

     private _opened: boolean = false;

  private _toggleSidebar() {
    this._opened = !this._opened;
  }

问题是,按钮根本不反映在屏幕上,此外,没有任何事情发生。请帮忙解决这个问题。

2 个答案:

答案 0 :(得分:1)

尝试将您的HTML更改为:

<div class="page-header custom-phead">
  <ng-sidebar-container>
    <ng-sidebar [(opened)]="_opened">
      <p>Sidebar contents</p>
    </ng-sidebar>
    <!-- you can also try to add a div below if your content not visible -->
    <div style="clear:both;"></div>
  </ng-sidebar-container>

  <button (click)="_toggleSidebar()">Toggle</button>

  <form class="heading">
    <input type="text" name="search" [(ngModel)]="search">
    <input type="image" [src]="'../resource/searchicon.png'">
  </form>
</div>

请注意,我从<button (click)="_toggleSidebar()">Toggle</button>中取出了ng-sidebar-container。如果侧边栏内容仍然无法显示,请在关闭<div style="clear:both;"></div>代码之前尝试添加ng-sidebar-container

我创建了一个plunker,它使用这两种方法切换侧边栏:[(opened)]="_opened"close()以及open()个功能。它可以帮助您完成项目

答案 1 :(得分:1)

Andriy解决方案不适用于Angular6,因此请在此处添加我的答案。

如官方文档的ReadME.md中所述,您需要提及侧边栏容器的高度元素。

给出的代码对我有用

app.component.html

<div>
    <ng-sidebar-container style=" height: 100vh">
    <ng-sidebar [(opened)]="_opened" mode="push" autoCollapseWidth=100>

      <p>Sidebar contents</p>
      <p>Sidebar contents</p>
      <p>Sidebar contents</p>
      <p>Sidebar contents</p>
      <p>Sidebar contents</p>

    </ng-sidebar>

    <!-- Page content -->
    <div ng-sidebar-content>
      <button (click)="_toggleSidebar()">Toggle sidebar</button>
    </div>

  </ng-sidebar-container>    
</div>

app.component.ts

public _opened: boolean = false;

public _toggleSidebar() {
this._opened = !this._opened;
}
在app.module.ts中

将sideBar导入为

imports: [
    SidebarModule.forRoot()
]