【发布时间】:2022-03-10 21:06:24
【问题描述】:
我在另一个组件中创建了对话框作为组件。对话框打开没有问题,但在关闭并尝试重新打开后它不可见。
父组件
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() {
}
}
父级html
<app-post [display]="display"></app-post>
<button type="button" class="btn btn-primary col"
(click)="openAdvancedSearch()" [style.width.px]="150">Advanced Search</button>
对话框组件
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() {
}
}
Dialog 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>
【问题讨论】:
-
感谢我能够解决问题输出 EventEmitter。关键是只从父组件而不是从子组件修改显示属性的值。当 close 被调用时,会生成一个事件,该事件将被父级拦截,并将 display 的值设置为 false
-
您可以在答案中写下相同的内容并接受它,这样其他人就不会知道您的问题是否已经解决了。
标签: angular modal-dialog primeng