Ali Baig
Ali Baig

Reputation: 3867

p-dialog onHide not working in angular 2 component - primeng

I'm using primeng in an angular 2 application and facing this issue (stackoverflow question)

Although the plunkr provided in the accepted answer works but it doesn't in my scenario. I have a separate component that loads the based on an input from the parent component. I want to toggle the visibility flag when the child component is closed/hidden.

Here's the code snippet

 <p-dialog header="Assets Management" [(visible)]="showDialog" modal="modal" [closable]="true" (onHide)="close()" appendTo="body">
          .. some content ..
  </p-dialog>

In component, I have:

@Component({
    selector: 'view-car-colors',
    templateUrl: '/view-car-colors.html',
    inputs: ['showDialog'],
    outputs: ["onCloseDialog"],
})
export class ViewCarColorsComponent {
    private showDialog: boolean = false;    //default close
    private onCloseDialog: EventEmitter<any> = new EventEmitter();

    public close(): void {
        this.showDialog = false;
        //emit this to its parent
        this.onCloseDialog.emit({ hasChanges: true });
    }
}

And finally in my parent component, I am calling it like:

<view-car-colors [showDialog]="showCarColorsDialog" (onCloseDialog)="onCarColorsCloseDialog($event)"></view-car-colors>

Where showCarColorsDialog is changed based on a button click.

private onCarColorsCloseDialog($event: any): void {
    this.showCarColorsDialog = false;
    if ($event.hasChanges) {
        //fetch the changes again
        this.getCarColors();
    }
}

I have used the primeng controls on multiple places and they all work fine but just has this issue so I'm sure it can't be because of the version.

Upvotes: 9

Views: 20530

Answers (6)

Sameer
Sameer

Reputation: 5188

In my case someone added *ngIf as well to dialog, and as you know *ngIf will remove dialog from the DOM, so onHide is never called...I had to remove the *ngIf from dialog(See example below)

<!------- ↓Remove this *ngIf-->
<p-dialog *ngIf="display" [(visible)]="display">
 ...
</p-dialog>

Upvotes: 0

Geordie Jenner
Geordie Jenner

Reputation: 1

use a get/set

public _displayDialog: boolean = false;
get displayDialog() { return this._displayDialog; }
set displayDialog(value) {
  this._displayDialog = value;
  if (this._displayDialog == false) {
    alert("hide")
  }
};

Upvotes: 0

Mohammad Almasi
Mohammad Almasi

Reputation: 420

Try

<p-dialog [(visible)]="displayDialog" appendTo="body">

Upvotes: -1

XeNo13GrIn
XeNo13GrIn

Reputation: 122

try (onAfterHide)="close()".

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

Upvotes: 4

Ali Baig
Ali Baig

Reputation: 3867

After onHide didn't worked, I found a workaround using getter/setter like:

In my child component:

private _showDialog: boolean = false;

set showDialog(_show: boolean) {
        let result: boolean = this._showDialog != _show;
        if (result == true) {
            this._showDialog = _show;
            this.onCloseDialog.emit({ hasChanges: this.hasChanges, state: this._showDialog });
        }
    }
    get showDialog(): boolean{
        return this._showDialog;
    }

And in parent template:

<!--View Car Colors Dialog In Parent Component-->
<view-car-colors [showDialog]="showCarColorsDialog" (onCloseDialog)="onCarColorsCloseDialog($event)"></view-car-colors>

In Component, I receive the emit event:

private onCarColorsCloseDialog($event: any): void {
    let result = this.showCarColorsDialog != $event.state;
    if (result == true) {
        this.showCarColorsDialog = $event.state;
        if ($event.hasChanges) {
            this.getCarColors();
        }
    }
}

Upvotes: 1

Tima Root
Tima Root

Reputation: 11

try to implement:

export class ViewCarColorsComponent {
    @Output() onCloseDialog: EventEmitter<any> = new EventEmitter<any>();
.
.
.

}

and change modal="modal" to modal="true" in html file

Upvotes: 0

Related Questions