问题描述
我有一个CustomDialog,并将其用作确认对话框,并将其外部组件显示为模式对话框。 我想注入在其中的TS文件中创建的动态formGroup。在使用Metro4组件之前,该组件在对话框服务中具有如下方法:
sftp
这是TS代码:
<mat-dialog-content>
<ng-template #renderComponent>'I used this section to show component'</ng-template>
<div *ngIf="data.form" [formGroup]="form">
'I want to inject formgroup hier'
'like injecting as an formGroup in data.form parameter'
</div>
</mat-dialog-content>
但是现在我想使用Angular Material UI,并且想要创建上面提到的东西。 我想知道我该怎么做。
例如,我这样调用我的CustomDialog:
import { FormGroup } from '@angular/forms';
import { AfterViewInit,Component,Inject } from "@angular/core";
import { MatDialogRef,MAT_DIALOG_DATA } from "@angular/material/dialog";
import { ViewChild,ComponentFactoryResolver,ViewContainerRef,ComponentRef } from "@angular/core";
@Component({
selector: 'custom-dialog',templateUrl: './custom-dialog.component.html',styleUrls: ['./custom-dialog.component.scss']
})
export class CustomDialogComponent implements AfterViewInit {
@ViewChild("renderComponent",{ read: ViewContainerRef })
vcRef: ViewContainerRef;
componentRef: ComponentRef<any>;
form: FormGroup;
constructor(public dialogRef: MatDialogRef<CustomDialogComponent>,private resolver: ComponentFactoryResolver,@Inject(MAT_DIALOG_DATA) public data: any) {
}
ngAfterViewInit() {
const comp = this.resolver.resolveComponentFactory(this.data.component);
this.componentRef = this.vcRef.createComponent(comp);
}
ngOnDestroy() {
if (this.componentRef) {
this.componentRef.destroy();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)