当从另一个组件调用时,PrimeNG角度2模态不起作用

时间:2017-01-13 22:55:26

标签: angular primeng

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

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

(string) $value

对话框代码基本上是来自doc's的复制粘贴

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

,html代码在这里

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() {
  }

}

关于调试我发现显示的SwatModalComponent属性设置为true,但屏幕上没有显示模态。

2 个答案:

答案 0 :(得分:1)

我有同样的问题,以下双向绑定对我有用。无需共享服务。

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

    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。

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

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

    ...  
        selectedItem: ItemClass; // selected by table
    
        changeItem() {
            this.displayEditDialog = true;
        }
    ...
    

    希望这有帮助。

答案 1 :(得分:0)

所以万一可以帮助某人

为对话框创建一个新组件,并将其选择器放在包装器组件html文件

<app-mac-dialog></app-mac-dialog>

现在您需要某种事件来触发该组件 我使用带有EventEmitter的共享服务将数据传递给对话框组件,并基本上将其display属性设置为true

相关问题