import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
@Component({
selector: 'app-notification',
templateUrl: './notification.component.html',
styleUrls: ['./notification.component.css']
})
export class NotificationComponent implements OnInit {
constructor(public toastr: ToastsManager, vcr: ViewContainerRef) {
this.toastr.setRootViewContainerRef(vcr);
}
showSuccess() {
this.toastr.success('You are awesome!', 'Success!');
}
showError() {
this.toastr.error('This is not good!', 'Oops!');
}
showWarning() {
this.toastr.warning('You are being warned.', 'Alert!');
}
showInfo() {
this.toastr.info('Just some information for you.');
}
showCustom() {
this.toastr.custom('<span style="color: red">Message in red.</span>', null, {enableHTML: true});
}
ngOnInit() {
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Angular2</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="node_modules/toastr/build/toastr.css">
</head>
<body>
<app-root>Loading...</app-root>
<script src="node_modules/toastr/build/toastr.min.js"></script>
</body>
</html>
{
"project": {
"version": "1.0.0-beta.28.3",
"name": "angular2"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"prefix": "app",
"styles": [
"styles.css",
"../node_modules/ng2-toastr/ng2-toastr.css"
],
"scripts": [],
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"files": "src/**/*.ts",
"project": "src/tsconfig.json"
},
{
"files": "e2e/**/*.ts",
"project": "e2e/tsconfig.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"prefixInterfaces": false,
"inline": {
"style": false,
"template": false
},
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
}
<button class="btn btn-default" (click)="showSuccess()">Toastr Tester</button>