ngx-toasta
An angularX toast component that shows growl-style alerts and messages for your application.
Follow me
to be notified about new releases.
Installation
npm install ngx-toasta
Demo
Online demo available here
Usage
If you use SystemJS to load your files, you might have to update your config:
System.config({
map: {
'ngx-toasta': 'node_modules/ngx-toasta/bundles/index.umd.js'
}
});
1. Update the markup
- Import style into your web page. Choose one of the following files;
style-default.css
- Contains DEFAULT themestyle-bootstrap.css
- Contains Bootstrap 3 themestyle-material.css
- Contains Material Design theme
- Assign the selected theme name [
default
, bootstrap
, material
] to the theme
property of the instance of ToastaConfig. - Add
<ngx-toasta></ngx-toasta>
tag in template of your application component.
2. Import the ToastaModule
Import ToastaModule.forRoot()
in the NgModule of your application.
The forRoot
method is a convention for modules that provide a singleton service.
import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from '@angular/core';
import {ToastaModule} from 'ngx-toasta';
@NgModule({
imports: [
BrowserModule,
ToastaModule.forRoot()
],
bootstrap: [AppComponent]
})
export class AppModule {
}
If you have multiple NgModules and you use one as a shared NgModule (that you import in all of your other NgModules),
don't forget that you can use it to export the ToastaModule
that you imported in order to avoid having to import it multiple times.
@NgModule({
imports: [
BrowserModule,
ToastaModule.forRoot()
],
exports: [BrowserModule, ToastaModule],
})
export class SharedModule {
}
3. Use the ToastaService
for your application
- Import
ToastaService
from ngx-toasta
in your application code:
import {Component} from '@angular/core';
import {ToastaService, ToastaConfig, ToastOptions, ToastData} from 'ngx-toasta';
@Component({
selector: 'app',
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ngx-toasta></ngx-toasta>
`
})
export class AppComponent {
constructor(private toastaService:ToastaService, private toastaConfig: ToastaConfig) {
this.toastaConfig.theme = 'material';
}
addToast() {
this.toastaService.default('Hi there');
var toastOptions:ToastOptions = {
title: "My title",
msg: "The message",
showClose: true,
timeout: 5000,
theme: 'default',
onAdd: (toast:ToastData) => {
console.log('Toast ' + toast.id + ' has been added!');
},
onRemove: function(toast:ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
}
};
this.toastaService.info(toastOptions);
this.toastaService.success(toastOptions);
this.toastaService.wait(toastOptions);
this.toastaService.error(toastOptions);
this.toastaService.warning(toastOptions);
}
}
4. How to dynamically update title and message of a toast
Here is an example of how to dynamically update message and title of individual toast:
import {Component} from '@angular/core';
import {ToastaService, ToastaConfig, ToastaComponent, ToastOptions, ToastData} from 'ngx-toasta';
import {Subject, Observable, Subscription} from 'rxjs/Rx';
@Component({
selector: 'app',
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ngx-toasta></ngx-toasta>
`
})
export class AppComponent {
getTitle(num: number): string {
return 'Countdown: ' + num;
}
getMessage(num: number): string {
return 'Seconds left: ' + num;
}
constructor(private toastaService:ToastaService) { }
addToast() {
let interval = 1000;
let timeout = 5000;
let seconds = timeout / 1000;
let subscription: Subscription;
let toastOptions: ToastOptions = {
title: this.getTitle(seconds),
msg: this.getMessage(seconds),
showClose: true,
timeout: timeout,
onAdd: (toast: ToastData) => {
console.log('Toast ' + toast.id + ' has been added!');
let observable = Observable.interval(interval).take(seconds);
subscription = observable.subscribe((count: number) => {
toast.title = this.getTitle(seconds - count - 1);
toast.msg = this.getMessage(seconds - count - 1);
});
},
onRemove: function(toast: ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
subscription.unsubscribe();
}
};
switch (this.options.type) {
case 'default': this.toastaService.default(toastOptions); break;
case 'info': this.toastaService.info(toastOptions); break;
case 'success': this.toastaService.success(toastOptions); break;
case 'wait': this.toastaService.wait(toastOptions); break;
case 'error': this.toastaService.error(toastOptions); break;
case 'warning': this.toastaService.warning(toastOptions); break;
}
}
}
5. How to close specific toast
Here is an example of how to close an individual toast:
import {Component} from '@angular/core';
import {ToastaService, ToastaConfig, ToastaComponent, ToastOptions, ToastData} from 'ngx-toasta';
import {Subject, Observable, Subscription} from 'rxjs/Rx';
@Component({
selector: 'app',
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ngx-toasta></ngx-toasta>
`
})
export class AppComponent {
getTitle(num: number): string {
return 'Countdown: ' + num;
}
getMessage(num: number): string {
return 'Seconds left: ' + num;
}
constructor(private toastaService:ToastaService) { }
addToast() {
let interval = 1000;
let subscription: Subscription;
let toastOptions: ToastOptions = {
title: this.getTitle(0),
msg: this.getMessage(0),
showClose: true,
onAdd: (toast: ToastData) => {
console.log('Toast ' + toast.id + ' has been added!');
let observable = Observable.interval(interval);
subscription = observable.subscribe((count: number) => {
toast.title = this.getTitle(count);
toast.msg = this.getMessage(count);
if (count > 10) {
this.toastaService.clear(toast.id);
}
});
},
onRemove: function(toast: ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
subscription.unsubscribe();
}
};
switch (this.options.type) {
case 'default': this.toastaService.default(toastOptions); break;
case 'info': this.toastaService.info(toastOptions); break;
case 'success': this.toastaService.success(toastOptions); break;
case 'wait': this.toastaService.wait(toastOptions); break;
case 'error': this.toastaService.error(toastOptions); break;
case 'warning': this.toastaService.warning(toastOptions); break;
}
}
}
6. Customize the ngx-toasta
for your application in template
You can use the following properties to customize the ngx-toasta component in your template:
position
- The window position where the toast pops up. Default value is bottom-right
. Possible values: bottom-right
, bottom-left
, bottom-fullwidth
top-right
, top-left
, top-fullwidth
,top-center
, bottom-center
, center-center
Example:
<ngx-toasta [position]="'top-center'"></ngx-toasta>
7. Options
Use these options to configure individual or global toasts
Options specific to an individual toast:
ToastOptions
{
"title": string,
"msg": string,
"showClose": true,
"showDuration": true,
"theme": "default",
"timeout": 5000,
"onAdd": Function,
"onRemove": Function
}
Configurations that affects all toasts:
ToastaConfig
{
"limit": 5,
"showClose": true,
"showDuration": true,
"position": "bottom-right",
"timeout": 5000,
"theme": "default"
}
Credits
Original work by ng2-toasty
License
MIT