如何在Angular中使用bootstrap崩溃

时间:2019-02-27 16:24:50

标签: angular bootstrap-4

我是角度新手,我有两个折叠div元素,当我单击button1时,div元素1需要折叠,而element-2需要隐藏,当我单击button2时,div element-2需要折叠和element-1需要隐藏,但无法使用以下代码工作,请有人帮忙

<div class="container">
  <h2>Simple Collapsible</h2>
  <button type="button" class="btn btn-info" (click)="selectItem='one'" data-toggle="collapse" data-target="#demo1">Simple
    collapsible</button>
  <button type="button" class="btn btn-info" (click)="selectItem='two'" data-toggle="collapse" data-target="#demo2">Simple
    collapsible</button>

  <div [ngClass]="(selectItem=='one')?'visiable':'hide'">
    <div id="demo1" class="collapse">
      <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</h2>
    </div>
  </div>

  <div [ngClass]="(selectItem=='two')?'visiable':'hide'">
    <div id="demo2" class="collapse">
      Lorem ipsum dolor sit amet, consectetur adipisicing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
  </div>
</div>

https://stackblitz.com/edit/angular5-bootstrap4-crud-device-list-simple-zxunj1?file=app%2Fapp.component.html

2 个答案:

答案 0 :(得分:0)

我建议您看一下这个库: ng bootstrap

没有这个,我无法使折叠工作 优点是不需要其他JavaScript

答案 1 :(得分:0)

您应该在show div上切换collapse类...

  <div>
    <div id="demo1" class="collapse" [ngClass]="{'show':selectItem=='one'}">
      <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</h2>
    </div>
  </div>

  <div>
    <div id="demo2" class="collapse" [ngClass]="{'show':selectItem=='two'}">
      Lorem ipsum dolor sit amet, consectetur adipisicing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
      quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
  </div>

https://stackblitz.com/edit/angular5-bootstrap4-crud-device-list-simple-wwbgwd?file=app/app.component.html