[英]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)我在 Angular 2 应用程序中使用 primeng 并面临这个问题(stackoverflow 问题)
Although the plunkr provided in the accepted answer works but it doesn't in my scenario.尽管在接受的答案中提供的 plunkr 有效,但在我的场景中却没有。 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. showCarColorsDialog
根据按钮单击更改的位置。
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.我在多个地方使用了primeng控件,它们都可以正常工作,但只是有这个问题,所以我确定这不可能是因为版本。
try (onAfterHide)="close()"
.尝试
(onAfterHide)="close()"
。
https://github.com/primefaces/primeng/issues/956 https://github.com/primefaces/primeng/issues/956
After onHide
didn't worked, I found a workaround using getter/setter like:在
onHide
后,我找到了使用 getter/setter 的解决方法,例如:
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();
}
}
}
try to implement:尝试实施:
export class ViewCarColorsComponent {
@Output() onCloseDialog: EventEmitter<any> = new EventEmitter<any>();
.
.
.
}
and change modal="modal" to modal="true" in html file并在 html 文件中将 modal="modal" 更改为 modal="true"
尝试
<p-dialog [(visible)]="displayDialog" appendTo="body">
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")
}
};
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)在我的情况下,有人也将
*ngIf
添加到对话框中,并且您知道*ngIf
将从 DOM 中删除对话框,因此永远不会调用onHide
...我必须从对话框中删除*ngIf
(请参见下面的示例)
<!------- ↓Remove this *ngIf-->
<p-dialog *ngIf="display" [(visible)]="display">
...
</p-dialog>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.