Last active
September 17, 2024 21:54
-
-
Save mrgoos/8bffca32eef7ae6c143b99d069145651 to your computer and use it in GitHub Desktop.
Use primeNg GrowlModule globally through a service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p-growl [value]="msgs"></p-growl> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, OnDestroy } from '@angular/core'; | |
import { Message } from 'primeng/primeng'; | |
import { NotificationsService } from './notifications.service'; | |
import { Subscription } from 'rxjs/Subscription'; | |
@Component({ | |
selector: 'app-notifications', | |
templateUrl: './notifications.component.html', | |
styleUrls: ['./notifications.component.css'] | |
}) | |
export class NotificationsComponent implements OnInit, OnDestroy { | |
msgs: Message[] = []; | |
subscription: Subscription; | |
constructor(private notificationsService: NotificationsService) { } | |
ngOnInit() { | |
this.subscribeToNotifications(); | |
} | |
subscribeToNotifications() { | |
this.subscription = this.notificationsService.notificationChange | |
.subscribe(notification => { | |
this.msgs.length = 0; | |
this.msgs.push(notification); | |
}); | |
} | |
ngOnDestroy() { | |
this.subscription.unsubscribe(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Subject } from 'rxjs/Subject'; | |
type Severities = 'success' | 'info' | 'warn' | 'error'; | |
@Injectable() | |
export class NotificationsService { | |
notificationChange: Subject<Object> = new Subject<Object>(); | |
notify(severity: Severities, summary: string, detail: string) { | |
this.notificationChange.next({ severity, summary, detail }); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NotificationsService } from 'PATH_TO_NOTIFICATIONS_SERVICE'; | |
.... | |
.... | |
constructor(private notificationsService: NotificationsService) { } | |
.... | |
.... | |
ngOnInit() { | |
this.notificationsService.notify('info', 'some component', 'ngOnInit was called!'); | |
} | |
.... | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//instantiate the notification component | |
<app-notifications></app-notifications> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//just import the service in order to provide it | |
.... | |
.... | |
import { NotificationsService } from './notifications/notifications.service'; | |
.... | |
.... | |
@NgModule({ | |
imports: [ | |
CommonModule | |
... | |
], | |
declarations: [ | |
TopComponent | |
], | |
providers: [NotificationsService] | |
}) | |
export class TopModule { | |
} |
Hello, what did you mean by 'top-component.html', because if it is the 'app.component.html' and you put the tag in there. If you try to use in other modules will not work. Right?
Sorry for my bad english.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, @rahulmatty Did you import the module on your app.module.ts?