Angular 2 Toasty
Angular2 Toasty component shows growl-style alerts and messages for your application.
Follow me to be notified about new releases.
Some of these APIs and Components are not final and are subject to change!
Installation
npm install ng2-toasty --save
Demo
Simple examples using ng2-dnd:
Online demo available here
Usage
If you use SystemJS to load your files, you might have to update your config:
System.config({
map: {
'ng2-toasty': 'node_modules/ng2-toasty'
},
packages: {
'ng2-toasty': { main: 'index.js', defaultExtension: 'js' },
}
});
Finally, you can use ng2-toasty in your Angular 2 project:
- Import
ToastyService, ToastyConfig, ToastyComponent, ToastOptions
from ng2-toasty
- Instantiate
ToastyService, ToastyConfig
in the bootstrap of your application; - Add
ToastyComponent
to the "directives" property of your application component; - Add
<ng2-toasty></ng2-toasty>
tag in template of your application component. - Inject 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 to the
theme
property of the instance of ToastyConfig.
import {Component} from '@angular/core';
import {ToastyService, ToastyConfig, ToastyComponent, ToastOptions, ToastData} from 'ng2-toasty';
@Component({
selector: 'app',
directives: [ToastyComponent],
providers: [ToastyService, ToastyConfig],
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ng2-toasty></ng2-toasty>
`
})
export class AppComponent {
constructor(private toastyService:ToastyService, private toastyConfig: ToastyConfig) {
this.toastyConfig.theme = 'material';
}
addToast() {
this.toastyService.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.toastyService.info(toastOptions);
this.toastyService.success(toastOptions);
this.toastyService.wait(toastOptions);
this.toastyService.error(toastOptions);
this.toastyService.warning(toastOptions);
}
}
How 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 {ToastyService, ToastyConfig, ToastyComponent, ToastOptions, ToastData} from 'ng2-toasty';
import {Subject, Observable, Subscription} from 'rxjs/Rx';
@Component({
selector: 'app',
directives: [ToastyComponent],
providers: [ToastyService, ToastyConfig],
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ng2-toasty></ng2-toasty>
`
})
export class AppComponent {
getTitle(num: number): string {
return 'Countdown: ' + num;
}
getMessage(num: number): string {
return 'Seconds left: ' + num;
}
constructor(private toastyService:ToastyService) { }
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.toastyService.default(toastOptions); break;
case 'info': this.toastyService.info(toastOptions); break;
case 'success': this.toastyService.success(toastOptions); break;
case 'wait': this.toastyService.wait(toastOptions); break;
case 'error': this.toastyService.error(toastOptions); break;
case 'warning': this.toastyService.warning(toastOptions); break;
}
}
}
How to close specific toast
Here is an example of how to close an individual toast:
import {Component} from '@angular/core';
import {ToastyService, ToastyConfig, ToastyComponent, ToastOptions, ToastData} from 'ng2-toasty';
import {Subject, Observable, Subscription} from 'rxjs/Rx';
@Component({
selector: 'app',
directives: [ToastyComponent],
providers: [ToastyService, ToastyConfig],
template: `
<div>Hello world</div>
<button (click)="addToast()">Add Toast</button>
<ng2-toasty></ng2-toasty>
`
})
export class AppComponent {
getTitle(num: number): string {
return 'Countdown: ' + num;
}
getMessage(num: number): string {
return 'Seconds left: ' + num;
}
constructor(private toastyService:ToastyService) { }
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.toastyService.clear(toast.id);
}
});
},
onRemove: function(toast: ToastData) {
console.log('Toast ' + toast.id + ' has been removed!');
subscription.unsubscribe();
}
};
switch (this.options.type) {
case 'default': this.toastyService.default(toastOptions); break;
case 'info': this.toastyService.info(toastOptions); break;
case 'success': this.toastyService.success(toastOptions); break;
case 'wait': this.toastyService.wait(toastOptions); break;
case 'error': this.toastyService.error(toastOptions); break;
case 'warning': this.toastyService.warning(toastOptions); break;
}
}
}
Credits
Inspired by angular-toasty
License
MIT