-
-
Save mrgoos/8bffca32eef7ae6c143b99d069145651 to your computer and use it in GitHub Desktop.
<p-growl [value]="msgs"></p-growl> |
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(); | |
} | |
} |
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 }); | |
} | |
} |
import { NotificationsService } from 'PATH_TO_NOTIFICATIONS_SERVICE'; | |
.... | |
.... | |
constructor(private notificationsService: NotificationsService) { } | |
.... | |
.... | |
ngOnInit() { | |
this.notificationsService.notify('info', 'some component', 'ngOnInit was called!'); | |
} | |
.... | |
.... |
//instantiate the notification component | |
<app-notifications></app-notifications> |
//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 { | |
} |
@HermenOtter, The PrimeNG docs are not great for implementing their messaging service. In a further up component (e.g. app.component), you need to add the message service as a provider, add the service in the constructor, and then add a growl component in your HTML which references your component's instance of the service, such as is shown here:
https://stackoverflow.com/questions/50410239/angular-2-primeng-message-service-not-showing-message
For me, PrimeNG's built in messaging service did not work until I followed these steps, at least.
@hoffmast I am trying to do what you described with the Toast component and the MessageService, as described in the (poor) documentation (https://www.primefaces.org/primeng/#/toast).
So, as you said, the MessageService is defined as a provider in my app component, the toast component is in the HTML of the app component as well, but I cannot see how to link it to a particular component further down in the hierarchy.
It goes without saying that I don't want to add the toast component on every single component's template that is using it. I want to add it once in one component (e.g. the app component) and reference it from there, pretty much as it's done in this example. But this example uses the NotificationService, subscriptions, and messages, not the MessageService, which, according to the documentation at least, should not require any such extra steps.
What am I missing?
@MrGoss @filosofisto, :- Getting this error. Please suggest.
'app-notifications' is not a known element:
- If 'app-notifications' is an Angular component, then verify that it is part of this module.
- If 'app-notifications' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
@MrGoss @filosofisto, :- Getting this error. Please suggest.
'app-notifications' is not a known element:1. If 'app-notifications' is an Angular component, then verify that it is part of this module. 2. If 'app-notifications' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
Hi, @rahulmatty Did you import the module on your app.module.ts?
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.
If you look in the documentation for growl in change detection section :
So you either use the above code as is with [immutable]="false" or leave [immutable]="true" (default) and replace push with spread operator
this.growl = [...this.growl , notification]