ANGULAR2: USING Toasters AS ANGULAR SERVICE IN ANGULAR 2 APPLICATION

Standard

Since toasters are essential component for any connected web application, we have couple of toasters component available in market for angular2, I am using primeng toaster…

Usage

Since toasters are used throughout the application, so its better to define it once in index.html and then try to set/reset through component code. I am also using BehaviorSubject object in service, which is way more awesome and better then observable in many cases, Behavior Subject is a type of subject, a subject is a special type of observable so you can subscribe to messages like any other observable. In plain words when specific page is invoked, its ngOnit() method call the toaster service and pass the reference of toaster value to be load, then service set new value into its BehaviorSubject object(toasterStatus), all variables(objToaster) throughout the application subscribed to this BehaviorSubject object are then alerted and refresh their state with the new value.

Index/main-component.html code:

<p-growl [value]="objToaster"></p-growl>

main-component.ts:

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

import { Message } from 'primeng/primeng';
import { ToasterService } from './shared/service/alert.service';

@Component({
    selector: 'my-app',
    templateUrl: '../../Scripts/app/app.component.html'
})

export class AppComponent implements OnInit, AfterViewInit {
    objToaster: Message[];

    constructor(
        private toasterService: ToasterService) {
        this.objToaster = [];
    }

    ngOnInit() {
    }

    ngAfterViewInit() {
        this.toasterService.toasterStatus.subscribe((val: Message) => {
            if (val)
                this.objToaster.push(val);
        });
    }
}

toaster.service.ts:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { Message } from 'primeng/primeng';

@Injectable()
export class ToasterService {
    public toasterStatus: BehaviorSubject<Message> = new BehaviorSubject<Message>(null);

    //type: success, info, warn, error
    showToaster(type: string, header: string, content: string) {
        let toasterObj: Message = { severity: type, summary: header, detail: content };
        this.toasterStatus.next(toasterObj);
    }

    hideToaster() {
        this.toasterStatus.next(null);
    }
}

custom-component.ts:

import { Component, OnInit } from '@angular/core';
import { ToasterService } from '../../shared/service/alert.service';

@Component({
    selector: 'search-movie',
    templateUrl: '../../Scripts/app/search-movies/movie.component.html'
})

export class SearchMovieComponent implements OnInit {

    constructor(
        private toasterService: ToasterService) {
    }

    ngOnInit() {
        this.toasterService.showToaster("info", "Search Movie header", "custom msg");
    }
}

Output:

screenshot_15 screenshot_16

P.S.

Please include primeng package in app.module.ts

import { GrowlModule } from 'primeng/primeng';

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s