ANGULAR2: USING Alerts or notifications AS ANGULAR SERVICE IN ANGULAR 2 APPLICATION

Standard

Since alerts / notifications are essential component for any connected web application, we have couple of alerts / notifications component available in market for angular2, I am using ng-lightning notifications…

For custom alerts / notifications without using any third party lib, then checkout my other post :
https://hassantariqblog.wordpress.com/2017/03/22/angular2-custom-toaster-notification-service-using-angular2-material-snackbar-component/

Usage

Since alerts / notifications 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 alert service and pass the reference of alert value to be load, then service set new value into its BehaviorSubject object(alertStatus), all variables(objAlert) throughout the application subscribed to this BehaviorSubject object are then alerted and refresh their state with the new value.

Index/main-component.html code:

<span class="slds-notify_container">
     <ngl-notification *ngIf="objAlert.show" type="alert" severity="error" 
     (nglNotificationClose)="onCloseAlert($event)" timeout="5000" 
     class="slds-theme--alert-texture">
          <h2>{{objAlert.message}}</h2>
     </ngl-notification>
 </span>

main-component.ts:

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

import { AlertService, AlertMessage } from './shared/service/alert.service';


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

export class AppComponent implements OnInit {
    objAlert: AlertMessage;

    constructor(
        private alertService: AlertService) {
    }

    ngOnInit() {
        this.alertService.alertStatus.subscribe((val: AlertMessage) => {
            this.objAlert = { show: val.show, message: val.message };
        });
    }

    onCloseAlert(reason: string) {
        let objCloseAlert: AlertMessage = { show: false, message: '' };
        this.alertService.showAlert(false, null);
    }
}

alert.service.ts:

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

import { Message } from 'primeng/primeng';

export class AlertMessage {
    public show: boolean;
    public message: string;
}

@Injectable()
export class AlertService {
    public alertStatus: BehaviorSubject<AlertMessage> = new BehaviorSubject<AlertMessage>({ show: false, message: null });

    showAlert(isShow: boolean, msg: string) {
        let alertObj: AlertMessage = { show: isShow, message: msg };
        this.alertStatus.next(alertObj);
    }
}

custom-component.ts:

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

import { AlertService } from '../../shared/service/alert.service';

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

export class SearchMovieDetailComponent implements OnInit {

    constructor(
        private alertService: AlertService) {        
    }

    ngOnInit() {
         this.alertService.showAlert(true, "Details of " + this.movieDetailModel.title + " has been laoded.");
    }
}

Output:

screenshot_13

screenshot_14

P.S.

Please include ng-lightning package in app.module.ts

import { NglModule } from 'ng-lightning/ng-lightning';

 

Advertisements

2 thoughts on “ANGULAR2: USING Alerts or notifications AS ANGULAR SERVICE IN ANGULAR 2 APPLICATION

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