我在angular 2应用程序中使用primeng,面对这个问题(stackoverflow question)
虽然接受答案中提供的plunkr有效,但在我的场景中不起作用。我有一个单独的组件,它根据来自父组件的输入加载。我希望在子组件关闭/隐藏时切换可见性标志。
下面是代码片段
<p-dialog header="Assets Management" [(visible)]="showDialog" modal="modal" [closable]="true" (onHide)="close()" appendTo="body">
.. some content ..
</p-dialog>
在组件中,我有:
@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 });
}
}
最后,在我的父组件中,我是这样调用它的:
<view-car-colors [showDialog]="showCarColorsDialog" (onCloseDialog)="onCarColorsCloseDialog($event)"></view-car-colors>
其中showCarColorsDialog
是基于单击按钮而更改的。
private onCarColorsCloseDialog($event: any): void {
this.showCarColorsDialog = false;
if ($event.hasChanges) {
//fetch the changes again
this.getCarColors();
}
}
我已经在多个地方使用了primeng控件,它们都工作得很好,但就是有这个问题,所以我确定不会是因为版本的原因。
发布于 2017-06-05 17:39:59
试试(onAfterHide)="close()"
。
发布于 2017-06-03 02:00:29
在onHide
不起作用后,我找到了一个使用getter/setter的变通方法,如下所示:
在我的子组件中:
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;
}
在父模板中:
<!--View Car Colors Dialog In Parent Component-->
<view-car-colors [showDialog]="showCarColorsDialog" (onCloseDialog)="onCarColorsCloseDialog($event)"></view-car-colors>
在组件中,我收到emit事件:
private onCarColorsCloseDialog($event: any): void {
let result = this.showCarColorsDialog != $event.state;
if (result == true) {
this.showCarColorsDialog = $event.state;
if ($event.hasChanges) {
this.getCarColors();
}
}
}
发布于 2019-11-27 15:05:03
尝试
<p-dialog [(visible)]="displayDialog" appendTo="body">
https://stackoverflow.com/questions/44065786
复制相似问题