2017-05-19 25 views
1

我使用primeng在角2应用程序和面对这个问题(stackoverflow question)对对话onHide没有角2组分工作 - primeng

虽然在接受答案的作品所提供的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控制,他们都工作得很好,但只是有这个问题,所以我敢肯定,这不可能是因为版本。

+0

是的,这是我的问题。你有没有找到解决方案? bcs仍然在为此而苦苦挣扎。 –

+0

@RameshRajendran我添加了我的答案,我是如何在下面做的选择。 –

回答

0

尝试实施:

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

} 

,改变模式= “莫代尔”,在HTML文件中模态= “真” onHide没有工作

+0

不,它没有工作 –

1

后,我发现了一个变通方法使用的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; 
    } 

而且在父母TEM板:

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

是啊!那很好。但是也可以隐藏关闭图标并禁用esc按钮:P:P。 –