【发布时间】:2019-03-21 08:30:47
【问题描述】:
我有一个 PopupComponent(在 Angular 库中),其中包含一些我正在使用的输入变量:
<lib-popup
[popupHeight] = "popupHeight"
[popupWidth] = "popupWidth"
[popupPositionX] = "popupPositionX"
[popupPositionY] = "popupPositionY"
>
</lib-popup>
现在我想在弹出窗口中插入我的MapComponent,这样当我打开弹出窗口时,它会显示一个地图。
我添加了另一个输入变量contentComponent,如下所示:
<lib-popup
[popupHeight] = "popupHeight"
[popupWidth] = "popupWidth"
[popupPositionX] = "popupPositionX"
[popupPositionY] = "popupPositionY"
[contentComponent] = "GeomapComponent"
>
</lib-popup>
我的popup.component.ts 看起来像这样:
import { Component, Input, OnInit } from '@angular/core';
import { MatDialog, MatDialogConfig } from '@angular/material';
import { PopupContentDefaultComponent } from './popup.content.default.component';
@Component({
selector: 'lib-popup',
templateUrl: './popup.component.html',
styleUrls: ['./popup.component.css']
})
export class PopupComponent implements OnInit {
@Input() popupPositionX = ''; // use default (px)
@Input() popupPositionY = ''; // use default (px)
@Input() popupHeight = '400px';
@Input() popupWidth = '600px';
@Input() popupOpen = false;
@Input() popupClose = false;
@Input() contentComponent = PopupContentDefaultComponent;
private dialogRef!: any;
private dialogConfig = new MatDialogConfig();
constructor(public dialog: MatDialog) {}
ngOnInit() {
// set config values for popup
this.dialogConfig.height = this.popupHeight;
this.dialogConfig.width = this.popupWidth;
this.dialogConfig.position = {
left: this.popupPositionX,
top: this.popupPositionY
};
if (this.popupOpen) {
this.openDialog();
}
if (this.popupClose) {
this.closeDialog();
}
}
openDialog(): void {
this.dialogRef = this.dialog.open(this.contentComponent, this.dialogConfig);
console.log('Popup has been opened.');
}
closeDialog(): void {
if (this.dialogRef) {
this.dialogRef.close();
console.log('Popup has been closed.');
} else {
console.log('There is no popup to close.');
}
}
}
到目前为止,一切正常!将打开一个弹出窗口,其中包含我的MapComponent。
现在的问题:
我想在弹出窗口中添加一个关闭按钮,无论我将哪个内容组件插入弹出窗口,该按钮始终存在。
所以我认为我需要一个额外的组件,比如PopupContentComponent,它包含每个弹出窗口应包含的按钮和其他一些内容,并且还有一个占位符用于我要显示的组件。
但是我如何在 Angular 中做到这一点?
我不想更改 MapComponent 并且弹出组件应该保留在我的库中。
【问题讨论】:
-
如果我没记错的话,你只需要用那个按钮关闭弹出窗口吗?为什么不将按钮 添加到弹出组件的 HTML 部分以及 *ngIf 中。当按下按钮时,函数会将 true 添加到某个变量,然后定义your components,然后将函数添加到组件 fun()= {showMe=true;} 中。也不要忘记在组件中定义它,例如 showMe:boolean = false;
标签: angular angular-material popup