10

How to call angular function when click p-dialog close(X) button?

I have searched and just tried this (onHide)="cancel()" . But it's not working. Kindly share your solutions.

I know we can use a close/cancel button to hide the popup. But in my scenario I want to call an event when clicking the (X) button click.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Did you fixed this issue? How you fixed it? – mkHun Nov 19 '18 at 13:27
  • @mkHun Tell me your scenario what you want...Because still it's not working for me. So I hvae changed my logic . – Ramesh Rajendran Nov 19 '18 at 13:42
  • I want to redirect to another page when click the close button. – mkHun Nov 19 '18 at 13:55
  • But same code in difference machine it is working, difference is version, I am using 6.2 angular version another system angular version is 6.1? Is it a problem? I am not sure. I am going to upgrade the angular 6 to 7. – mkHun Nov 19 '18 at 13:57
  • @mkHun I am not sure. May be we are getting the version problem. because If you upgrade angular, then you should upgrade Primeng version as well. – Ramesh Rajendran Nov 19 '18 at 13:59
  • Okay. I will check and inform you. – mkHun Nov 19 '18 at 13:59
  • Have you checked that it has anything to do with *ngIf in the p-dialog tag? See https://stackoverflow.com/questions/55000644/p-dialog-onhide-is-not-working-with-ngif?rq=1 – user2367418 Feb 20 '20 at 01:40

7 Answers7

10

Actually (onHide)="cancel()" works fine according to this Plunkr.

hiper2d
  • 2,814
  • 1
  • 18
  • 14
2

Try: (click)="cancel()" instead.

I had the same error but I solved it by using the click method. Grettings :)

1

You should use two events as follows:

onBeforeHide: EventEmitter<any>;
onAfterHide: EventEmitter<any>;

use in html as

(onBeforeHide)="onBeforeHide()"
(onAfterHide)="onAfterHide()"

Refer: https://github.com/primefaces/primeng/issues/956

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Trung
  • 1,819
  • 1
  • 13
  • 10
1

Just to add the above, If your [(visible)]="myBool_1 || myBool_2" depends on multiple variable.

Clicking X will try to set the last variable myBool_2 as false, we could leverage this by using a setter function.

so [(visible)]="isVisible"

class component {
 public get isVisible(): boolean {
    return myBool_1 || myBool_2
 }
 public set isVisible(val: boolean) {
   this.myBool_1 = this.myBool_2 = val;
   this.doSomethingOnClose() 
 }
}
Vaisakh Rajagopal
  • 1,189
  • 1
  • 14
  • 23
1

You can use the onHide EventEmitter, here the (alternative working method) sample code for the ts and html.

TS:

import {
  ...
  ViewChild,
  ...
} from '@angular/core';
import { Dialog } from 'primeng/dialog';  

...

@ViewChild('testDialog') testDialog: Dialog; 

...

onTestDialogClose() {
  const subscription = this.testDialog.onHide.asObservable().subscribe((_) => {
    // Do the action here
    subscription.unsubscribe();
  });
}

HTML:

<p-dialog #testDialog></p-dialog>
Snowbases
  • 2,316
  • 2
  • 20
  • 26
0

A workaround is to use a boolean to display the p-dialog with

   [(visible)]="myBoolean"

You set that boolean to true when you want to display the p-dialog Then use the (click) event. For instance

    (click)="doSomething($event)".

In your ts do

    doSomething(event) {
        // If we are clicking the close button and not something else
        if (event.target.className === "fa fa-fw fa-close") {
            myBoolean = false;
        }
    }
0

There is a simple fix, that is not documented in the docs but I found it in this issue. You just need to split two way binding into one way binding using visbile and visibleChange properties like this:

 <p-dialog header="My dialog" 
  [visible]="showDialog$ |async" 
  (visibleChange)="handleClose()"
 >

Above I use Angular's async pipe to show the dialog and handleClose() method to close the dialog. In the .ts file I can have a service injected which is using some observable of boolean value that is changing based on some condition.

export class HomeComponent implements OnInit {

  showDialog$!: Observable<boolean>;

  constructor(private homeService: HomeService) { }

  ngOnInit(): void {
    this.showDialog$ = this.homeService.displayDialog$;
  }
  handleClose() {
    this.homeService.toogleDisplayDialog();
  }
}
Lukas Coorek
  • 187
  • 1
  • 11