angular - 从另一个组件调用时,PrimeNG Angular 2 模态不起作用

标签 angular primeng

我有一个表,其中在组件中定义了许多行 我想实现这一点:当按下表格上的一行时,会出现一个模式(对话框)。 所以我为对话框创建了一个单独的组件,但它不起作用

表格组件代码在这里(相关部分)

import { SwatModalComponent } from '../swat-modal/swat-modal.component';

modal: SwatModalComponent;

  constructor(private alertService : AlertService) {
    if(alertService.filteredParam){
      //we have a filtered processAlertSwitchName
      this[alertService.filteredParam.name] = alertService.filteredParam.value;
      alertService.filteredParam = null;
    }
    this.registerEvents();
    this.modal = new SwatModalComponent();
  }

showModal() {
    this.modal.showDialog();
  } 

对话框代码基本上是从文档中复制粘贴的

import { Component, OnInit } from '@angular/core';

import {DialogModule} from 'primeng/primeng';

@Component({
  selector: 'app-swat-modal',
  templateUrl: './swat-modal.component.html',
  styleUrls: ['./swat-modal.component.sass']
})
export class SwatModalComponent implements OnInit {

  display: boolean = false;

    showDialog() {
        this.display = true;
    }

  constructor() { }

  ngOnInit() {
  }

}

html 代码在这里

<p-dialog header="Alert Dialog" [(visible)]="display" modal="modal" width="300" responsive="true">
    <header>
        Header content here
    </header>
    Content
    <footer>
        Footer content here
    </footer>
</p-dialog>

在调试时,我看到显示的 SwatModalComponent 属性被设置为 true,但屏幕上没有出现任何模式。

最佳答案

我遇到了同样的问题,以下双向绑定(bind)对我有用。无需共享服务。

  1. 从 primeng 基础对话框中捕获 onAfterClose 事件。
  2. 定义输出更改EventEmitter。
  3. 在 onAfterClose 处理程序中发出事件。

编辑对话框组件:

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'edit-dialog',
  templateUrl: './edit-dialog.component.html',
  styleUrls: ['./edit-dialog.component.css']
})
export class EditDialogComponent implements OnInit {


  @Input() item: ItemClass; // you entity to edit
  @Input() visible: boolean = false;
  @Output() visibleChange:EventEmitter<any> = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  afterHide() {
    this.visibleChange.emit(false);
  }
}

模板:

<p-dialog header="Godfather I" [(visible)]="visible" (onAfterHide)="afterHide()" modal="modal" responsive="true">
    <p>Some content....</p>
        <p-footer>
            <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
                <button type="button" pButton icon="fa-close" (click)="visible=false" label="Cancel"></button>
                <button type="button" pButton icon="fa-check" (click)="visible=false" label="OK"></button>
            </div>
        </p-footer>
</p-dialog>

然后,您可以通过在父组件的模板中实例化来使用 EditDialogComponent,而不是在 typescript 代码中。

<edit-dialog [(visible)]="displayEditDialog" [item]="selectedItem"></edit-dialog>

在组件的代码中,您可以通过将 display 属性设置为 true 来调用对话框。如果需要 EditDialogComponent,则无需导入。

...  
    selectedItem: ItemClass; // selected by table

    changeItem() {
        this.displayEditDialog = true;
    }
...

希望这有帮助。

关于angular - 从另一个组件调用时,PrimeNG Angular 2 模态不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41644725/

相关文章:

Angular PrimeNG p 表 : add custom sorting function to specific column

html - Angular 4 + Primeng(数据表): custom scrollHeight property

angular - Primeng 数据表 rowTrackBy

html - 响应式导航栏以 Angular 重叠在内容上

Angular2 为什么 getters 如果我们可以定义公共(public)属性

android - 如何在文本字段外触摸后隐藏键盘?

angular - 别名已被 "--collection"选项使用,不能被 "--change-detection"选项使用

javascript - Angular 2 + SignalR - 从外部脚本访问 Angular 内部

angular - 当我使用它来隐藏表列时,如何在多选 PrimeNG 中维护顺序?

html - CSS:选择具有特定类的最后一个 div