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-messages push method is not working for multiple messages #14507

Closed
deekshacse1999 opened this issue Jan 8, 2024 · 4 comments
Closed

P-messages push method is not working for multiple messages #14507

deekshacse1999 opened this issue Jan 8, 2024 · 4 comments
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible

Comments

@deekshacse1999
Copy link

Multiple messages was working in primeng 8 version but after migrating it to primeng v13, push method is not working to display multiple messages
We have Message[] everywhere in the application. But its not working post primeng v8

@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 8, 2024
@Cr3aHal0
Copy link
Contributor

Cr3aHal0 commented Jan 8, 2024

Could you provide a sample code or reproduction stackblitz about the current way you're using messages please ? Are you using MessageService or straight p-messages with input ?

@deekshacse1999
Copy link
Author

deekshacse1999 commented Jan 9, 2024

Hi
I am using p-messages directly with Value property for binding. Pls find below sample code.

Sample Code:
App.component.html
<p-messages [(value)]="msgs">

<button type="button" (click)="show()">Show
<button type="button" (click)="clear()">Hide

App.component.ts
import {Component, OnInit} from '@angular/core';
import {Message,MessageService} from 'primeng/api';

@Component({
    templateUrl: './messagesdemo.html',
    providers: [MessageService]
})
export class [Messages](https://www.primefaces.org/primeng-v13-lts/#/messages)Demo implements OnInit {

    msgs: Message[] = [];
    
    constructor() {}

    ngOnInit() {
        
    }

show() {
this.msgs.push({severity:'info', summary:'Info Message', detail:'PrimeNG rocks'});
}

hide() {
this.msgs = [];
}

}

@Cr3aHal0
Copy link
Contributor

Cr3aHal0 commented Jan 9, 2024

Because of p-messages being onPush & the value input not being used verbatim by the component (e.g. in a *ngFor) but being proxied by a setter, you may need to update msgs reference by submitting a new array of message rather than using Array.push. I'm pretty sure this was specified someday within the official documentation but I could not find it in a 5 minutes window.

A better approach (generally when working with onPush sub components) would be :

show() {
  this.msgs = [...this.msgs, {severity:'info', summary:'Info Message', detail:'PrimeNG rocks'}];
}

hide() {
  this.msgs = [];
}

@mertsincan
Copy link
Member

Thanks @Cr3aHal0 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible
Projects
None yet
Development

No branches or pull requests

3 participants