Open morsagmon opened 1 year ago
I found a workaround to make this work! Instead of having the dedicated NotificationComponent, I have moved the presentation of the toast task to the AppComponent. If you ask me - it should either be fixed to be supported from any component, or be EMPHASIZED in the documentation that it will only work if implemented in the app-root component. This is true to any use of the p-toast of PrimeNG that uses the MessagesApi service.
does also not work with p-messages. callings from a child-service or similar works, but in the standalone-component which provides the messageService, it can not be used.
okay, short after complaining I found the problem.
You cannot use the message-service in ngOnInit() because the view is not build yet and the messageservice cannot write to it. Use ngAfterViewInit for example to call the messageservice.add() method.
Using the variable binding function is no problem in the OnInit Method because the message is stored in the variable and read there after the view is initialised.
ngAfterViewInit() {
setTimeout(() => {
this.messageService.add({
key: 'success',
severity: 'success',
summary: title,
detail: description,
styleClass: 'bg-green-500',
contentStyleClass: 'p-3',
closable: false
});
});
}
This will work.
Describe the bug
Implementing a notification.component in Angular to serve notifications does not pop up the notifications. I suspect that the notifications implementation requires a single, app-wide, import of the MessageService in the app.module, however, as in a standalone Angular app there is no ngModule - it fails to serve the messages.
notification.component.html:
notification.component.ts:
The caller component:
NOTE: Removing the MessageService from the providers array in this caller component throws a runtime error in the console:
Additional notes
I tried to add the MessageService to the maint.ts file - to no avail:
appConfig:
Environment
Local dev env, vscode, Windows 10 machine.
Reproducer
https://stackblitz.com/edit/stackblitz-starters-sc2bnr?file=src%2Fapp%2Fpages%2Fsignup%2Fsignup.component.ts
https://stackblitz-starters-sc2bnr.stackblitz.io
Angular version
16.1.4
PrimeNG version
16.0.2
Build / Runtime
Angular CLI App
Language
TypeScript
Node version (for AoT issues node --version)
18.16.1
Browser(s)
No response
Steps to reproduce the behavior
Expected behavior
The notification popup to be served in the browser to the user. The popup does not show and no errors are given.