Reputation: 63
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 [email protected]
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?
Upvotes: 5
Views: 9420
Reputation: 1
Now the prop is named "closed" instead of "close".
@for (alert of alerts; track alert) {
<ngb-alert [type]="alert.type" (closed)="close(alert)">{{ alert.message }}</ngb-alert>
https://ng-bootstrap.github.io/#/components/alert/examples
Upvotes: 0
Reputation: 671
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>
Upvotes: 1
Reputation: 1267
You have to define a close
event method in your component.
<ngb-alert *ngIf="!closed" (close)="closed=true">Alert</ngb-alert>
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.
Upvotes: 17