Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<p-toast> is not working globally. #14422

Closed
tayyabroy786 opened this issue Dec 21, 2023 · 10 comments
Closed

<p-toast> is not working globally. #14422

tayyabroy786 opened this issue Dec 21, 2023 · 10 comments
Labels
Resolution: Invalid Issue or pull request is not valid in the latest version

Comments

@tayyabroy786
Copy link

Describe the bug

I have multiple modules with components. I have included the toaster in the shared module and am using it in the required modules. However, the issue I am facing is that I want to set the HTML globally in app.component.html or layout.component.html. Additionally, I want it to work in sub-components without having to add it to each sub-component's HTML file. Why do I need to add this to all 100 components?

Environment

Environment Information
Angular Version: [e.g., 16.1.0]
PrimeNG Version: [e.g., 16.0.2]
Browser(s): [e.g., Chrome, Firefox]
Operating System: [e.g., Windows 10]

Reproducer

No response

Angular version

16.1.0

PrimeNG version

16.0.2

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

16

Browser(s)

All

Steps to reproduce the behavior

You have multiple modules, each containing components. The toaster component is included in a shared module and is being utilized in the required modules. However, you are encountering an issue where you want to globally set the HTML in either app.component.html or layout.component.html. Additionally, you desire this global setting to automatically apply to sub-components without having to add it individually to each sub-component's HTML file. This becomes particularly cumbersome when dealing with a large number of components (in your example, 100 components), and you are questioning the necessity of manually adding this code to each one.

Expected behavior

No response

@tayyabroy786 tayyabroy786 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 21, 2023
@cetincakiroglu
Copy link
Contributor

Hi,

Could you please share a stackblitz example so we can identify the issue?

@mehmetcetin01140 mehmetcetin01140 added Resolution: Cannot Replicate Issue could not be replicated by Core Team and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Jan 5, 2024
Copy link

github-actions bot commented Jan 5, 2024

We're unable to replicate your issue, if you are able to create a reproducer by using PrimeNG Issue Template or add details please edit this issue. This issue will be closed if no activities in 20 days.

@tayyabroy786
Copy link
Author

tayyabroy786 commented Jan 5, 2024 via email

@cetincakiroglu
Copy link
Contributor

You don't need to add it to 100 places, just create a shared module or import at the top level.

1- import toast module to the top level module
2- add <p-toast></p-toast> at the top level component where the router outlet located.
3- use message service to display the toast.

It should be something like that:

<p-toast></p-toast>
<router-outlet></router-outlet>

@cetincakiroglu cetincakiroglu added Resolution: Invalid Issue or pull request is not valid in the latest version and removed Resolution: Cannot Replicate Issue could not be replicated by Core Team labels Jan 8, 2024
@tayyabroy786
Copy link
Author

tayyabroy786 commented Jan 8, 2024 via email

@yvkevitch
Copy link

I've encountered the same problem in angular 17 (standalone app).
You simply need to provide MessageService at the application level and not at the component level.

@Fourat-Khelifi
Copy link

@yvkevitch would you please explain more.
I tried but it's still the same.

@yvkevitch
Copy link

@Fourat-Khelifi
Add a p-toast component to the root page of your app (eg in you app component)
Add the MessageService to the providers of your app.config.ts file
In the components where you want to use toasts, inject MessageService (Don't add it to the providers of these components)
This way it should work like a charm

@reidecarvalho
Copy link

There's still a problem. Capturing the error globally with ErrorHandler and calling MessageService on it, Toast does not appear, only if I click on the screen.

import { ErrorHandler, Injectable, inject } from '@angular/core';
import { AppMessageService } from './services/app-message.service';

@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
  appMessageService = inject(AppMessageService);

  handleError(error: any): void {
    console.error('GlobalErrorHandler', error);

    this.appMessageService.error(
      `Erro processando solicitação:\n${error.message}`,
    );
  }
}
import { Injectable, inject } from '@angular/core';
import { MessageService } from 'primeng/api';

@Injectable({
  providedIn: 'root',
})
export class AppMessageService {
  private messageService = inject(MessageService);

  success(message: string): void {
    this.messageService.add({
      severity: 'success',
      summary: 'Sucesso',
      detail: message,
    });
  }

  info(message: string): void {
    this.messageService.add({
      severity: 'info',
      summary: 'Informação',
      detail: message,
    });
  }

  warn(message: string): void {
    this.messageService.add({
      severity: 'warn',
      summary: 'Atenção',
      detail: message,
    });
  }

  error(message: string): void {
    this.messageService.add({
      severity: 'error',
      summary: 'Erro',
      detail: message,
    });
  }
}

@emiliano-arango-nz
Copy link

emiliano-arango-nz commented May 16, 2024

Hi @reidecarvalho same issue here and I think it’s related to the ChangeDetectionStrategy for the toast component, which is using OnPush, so maybe a temp fix could be to force the change detection using:

constructor(private ref: ChangeDetectorRef) {} in your AppMessageService

and then:

this.ref.detectChanges(); in every method, for example:

error(message: string): void {
this.messageService.add({
severity: 'error',
summary: 'Erro',
detail: message,
});
this.ref.detectChanges();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Invalid Issue or pull request is not valid in the latest version
Projects
None yet
Development

No branches or pull requests

7 participants