5
votes

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.

5
Did you fixed this issue? How you fixed it? - mkHun
@mkHun Tell me your scenario what you want...Because still it's not working for me. So I hvae changed my logic . - Ramesh Rajendran
I want to redirect to another page when click the close button. - mkHun
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
@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

5 Answers

4
votes

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

1
votes

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

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

0
votes

You should to use two events follow:

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

use in html as

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

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

0
votes

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
votes

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() 
 }
}