p-dialog onHide不在角度2组件中工作 - primeng

时间:2017-05-19 09:04:17

标签: angular angular2-template primeng

我在角度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控件,但它们都运行良好但只是有这个问题所以我确定它不会因为版本而存在。

5 个答案:

答案 0 :(得分:3)

尝试(onAfterHide)="close()"

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

答案 1 :(得分:1)

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>

在Component中,我收到emit事件:

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

答案 2 :(得分:0)

尝试实施:

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

}

并更改模态=&#34;模态&#34; to modal =&#34; true&#34;在html文件中

答案 3 :(得分:0)

尝试

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

答案 4 :(得分:0)

使用获取/设置

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