ng-bootstrap

ng-bootstrap alerts won't close


My alerts won't close when I click the X button.

I've used angular cli to create a new app. I followed the instructions here to load bootstrap:

npm install bootstrap@4.0.0-alpha.6  

Then I followed instructions here to load ng-bootstrap:

npm install --save @ng-bootstrap/ng-bootstrap

app.module.ts:

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [
    AppComponent,
    NavComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

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


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Testing...';
}

app.component.html

<app-nav></app-nav>
<ngb-alert>Alert</ngb-alert>
...

The alert shows up, and is styled properly, but it doesn't close when I click the X. Other components are working (tabset, dropdown). Am I missing something in app.component.ts?


Solution

  • You have to define a close event method in your component.

    HTML:

    <ngb-alert *ngIf="!closed" (close)="closed=true">Alert</ngb-alert>
    

    Component:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'Testing...';
      closed = false;
    }
    

    I hope this will help you.