Home >>Ngx Bootstrap Tutorial >Ngx Bootstrap Collapse

Ngx Bootstrap Collapse

Ngx Bootstrap Collapse

The ngx-bootstrap Collapse directive enables you to show/hide content in a container.

CollapseDirective

selector

  • [collapse]

Inputs

  • collapse − boolean, A flag indicating visibility of content (shown or hidden)
  • display − string
  • isAnimated − boolean, turn on/off animation. default: false

Outputs

  • collapsed − This event fires as soon as content collapses
  • collapses − This event fires when collapsing is started
  • expanded − This event fires as soon as content becomes visible
  • expands − This event fires when expansion is started

Methods

  • toggle() − allows to manually toggle content visibility
  • hide − allows to manually hide content
  • show − allows to manually show collapsed content

Example

As we are going to use collapse, to use CollapseModule, we have to update app.module.ts used in the ngx-bootstrap Carousel chapter.

In order to use the CollapseModule, update app.module.ts.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert';
import { ButtonsModule } from 'ngx-bootstrap/buttons';
import { FormsModule } from '@angular/forms';
import { CarouselModule } from 'ngx-bootstrap/carousel';
import { CollapseModule } from 'ngx-bootstrap/collapse';

@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule,
      CarouselModule,
      CollapseModule
   ],
   providers: [AlertConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

Update test.component.html to use the Collapse.

test.component.html

<div>
   <div class="checkbox">
      <label><input type="checkbox" [(ngModel)]="isCollapsed">Collapse</label>
   </div>
</div>
<div [collapse]="isCollapsed" [isAnimated]="true">
   <div class="well well-lg card card-block card-header">Welcome to Tutorialspoint.</div>
</div>

Update test.component.ts for corresponding variables and methods.

test.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
   isCollapsed: boolean = false;
   constructor() { }

   ngOnInit(): void {
   }
}

Build and Serve

Run the following command to start the angular server.

ng serve