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

流过昼夜 提交于 2019-12-08 17:28:10

问题


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.


回答1:


try (onAfterHide)="close()".

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




回答2:


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



回答3:


try to implement:

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

}

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




回答4:


Try

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


来源:https://stackoverflow.com/questions/44065786/p-dialog-onhide-not-working-in-angular-2-component-primeng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!