ng-bootstrap alerts won't close

Viewed 8115

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?

2 Answers

This is very late but might help others looking for a simple closeable alert. This component wraps Devansh's solution:

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

@Component({
    selector: 'app-alert',
    template: '<ngb-alert [type]="type" *ngIf="open" (close)="open=false"><ng-content></ng-content></ngb-alert>',
})
export class InfoPanelComponent {
    public open = true;
    @Input() type = 'info';
}

usage:

<app-alert>I'm a closeable alert!</app-alert>
Related