【问题标题】:p-dialog onHide not working in angular 2 component - primengp-dialog onHide 不能在 angular 2 组件中工作-primeng
【发布时间】:2017-10-19 07:58:36
【问题描述】:

我在 angular 2 应用程序中使用primeng,并面临这个问题(stackoverflow question)

虽然在接受的答案中提供的 plunkr 有效,但在我的场景中却没有。我有一个单独的组件,它根据父组件的输入加载。我想在子组件关闭/隐藏时切换可见性标志。

这里是代码 sn-p

 <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控件,它们都可以正常工作,但只是有这个问题,所以我确定这不可能是因为版本。

【问题讨论】:

  • 是的,这是我的问题。你找到解决方案了吗? bcs 仍在为此苦苦挣扎。
  • @RameshRajendran 我在下面添加了关于我如何做到这一点的答案。

标签: angular angular2-template primeng


【解决方案1】:

尝试实施:

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

}

并在 html 文件中将 modal="modal" 更改为 modal="true"

【讨论】:

    【解决方案2】:

    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>
    

    在组件中,我收到了发出事件:

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

    【讨论】:

    • 是的!那挺好的。但也可以隐藏关闭图标并禁用 esc 按钮:P:P.
    【解决方案3】:

    试试(onAfterHide)="close()"

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

    【讨论】:

      【解决方案4】:

      试试

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

      【讨论】:

      【解决方案5】:

      使用获取/设置

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        相关资源
        最近更新 更多