primeng 对话框关闭后未打开

Viv*_*vek 6 modal-dialog primeng angular

我已将对话框创建为另一个组件内的组件。对话框打开没有问题,但关闭并尝试重新打开后它不可见。

父组件

import { Component,OnInit } from '@angular/core';
import { PostComponent } from './post/post.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  viewProviders: [PostComponent]
})
export class AppComponent implements OnInit {

  display: boolean;

  ngOnInit(): void {
  }
  
  openAdvancedSearch() {
    this.display = true;
    console.log("Display is set");
  }
 
  constructor() {
  }
}
Run Code Online (Sandbox Code Playgroud)

父 HTML

 <app-post [display]="display"></app-post>
  <button type="button" class="btn btn-primary col" 
                (click)="openAdvancedSearch()" [style.width.px]="150">Advanced Search</button>
       
Run Code Online (Sandbox Code Playgroud)

对话框组件

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

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

  @Input()
  display: boolean = false;
  publishedAfter: Date;
  publishedBefore:Date;

  constructor() { }

  ngOnInit() {
  }
}
Run Code Online (Sandbox Code Playgroud)

对话框 html 有一个按钮,单击该按钮可关闭对话框

  <p-dialog header="Title" [(visible)]="display" [width]="500" 
      [contentStyle]="{'overflow':'visible'}">
     <p-calendar [(ngModel)]="publishedAfter" [showIcon]="true" 
                    [style.width.px]="150"></p-calendar>

     <p-calendar [(ngModel)]="publishedBefore" [showIcon]="true" 
                    [style.width.px]="150"></p-calendar>

     <p-footer>
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <button (click)="display=false">Close</button>
      </div>  
     </p-footer>  
  </p-dialog>
Run Code Online (Sandbox Code Playgroud)

Viv*_*vek 2

谢谢我能够解决问题输出EventEmitter。关键是只能从父组件修改显示属性的值,而不能从子组件修改。当调用 close 时,会生成一个事件,该事件将被父级拦截,并将 display 的值设置为 false

  • @Vivek,您也可以提供您的答案吗?如何使用EventEmitter? (3认同)