我正在通过将html数据和函数绑定到动态组件来创建具有动态组件的运行时组件模块,该组件是我从api调用中获取的字符串数据。 然后将新生成的组件加载到放置在选项卡中的容器中,就像我根据数据动态创建选项卡一样。该过程适用于第一个标签,但不适用于其他标签。
这可能意味着,我在其中推送运行时组件的容器仅生成一次,因此不会反映其他选项卡。
HTML:
<mat-tab-group [selectedIndex]="tabIndex" (selectedTabChange)="getNewTabFields($event)">
<mat-tab *ngFor="let tab of tabs" [label]="tab.displayName">
<div *ngIf="errorOccured" class="text-align-center"> No Data Found! </div>
<ng-container #container></ng-container>
</mat-tab>
</mat-tab-group>
组件:
@ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;
ngOnInit() {
this.templateInitial =
this.searchConfig.configurations[0].value.resultView;
this.query = this.searchConfig.configurations[6].value.queryParams;
this.collectionName =
this.searchConfig.configurations[6].value.collectionName;
this.loadSearchData();
}
compileTemplate() {
const metadata = {
selector: `runtime-component`,
template: this.templateInitial
};
const _mydata = this.solrResponse.response.docs;
const code: string = this.searchConfig.configurations[1].value['logic'];
const result: string = ts.transpile(code);
const runnalbe: any = eval(result);
const compileClass = class RuntimeComponent {
solrResponses = _mydata;
presentLogic = runnalbe;
};
const factory = this.createComponentFactorySync(this.compiler, metadata, compileClass );
if (this.componentRef) {
this.container.remove(this.container.indexOf(this.componentRef));
this.componentRef = undefined;
}
this.componentRef = this.container.createComponent(factory);
}
private createComponentFactorySync(compiler: Compiler, metadata: Component, componentClass: any): ComponentFactory<any> {
const decoratedCmp = Component(metadata)(componentClass);
@NgModule({ imports: [CommonModule, SharedModule], declarations: [decoratedCmp] })
class RuntimeComponentModule { }
const module: ModuleWithComponentFactories<any> = compiler.compileModuleAndAllComponentsSync(RuntimeComponentModule);
this.componentInstance = module.componentFactories.find((f) => f.componentType === decoratedCmp);
return module.componentFactories.find((f) => f.componentType === decoratedCmp);
}
getNewTabFields(event) {
this.tabIndex = event.index;
this.constructTabData(this.tabIndex);
}
constructTabData(i) {
this.templateInitial = this.tabs[i].configurations.resultView;
this.query = this.tabs[i].configurations.query[0].queryParams;
this.collectionName = this.tabs[i].configurations.query[0].queryParams['collection'].split(' ')[0];
this.loadSearchData();
}
loadSearchData(_row = 10, _start = 0) {
this.solrSearchService.getSolrData(this.query, this.collectionName).subscribe((data: any) => {
this.solrResponse = [];
this.solrResponse = JSON.parse(data);
this.compileTemplate();
});
}
答案 0 :(得分:0)
我宁愿使用指令创建这些容器。
HTML:
<mat-tab-group [selectedIndex]="tabIndex" (selectedTabChange)="getNewTabFields($event)">
<div *ngIf="errorOccured" class="text-align-center"> No Data Found! </div>
<ng-container myDirective *ngFor="let tab of tabs" [tab]="tab"></ng-container>
</mat-tab-group>
我的指令:
@Directive({
selector: '[myDirective]'
})
export class MyDirective implements OnInit {
@Input()
tab: MyTamComponent;
constructor(private resolver: ComponentFactoryResolver, private container: ViewContainerRef) {}
ngOnInit(): void {
const factory = this.resolver.resolveComponentFactory<MyTamComponent>(MyTamComponent);
const component = this.container.createComponent(factory);
// other stuff
component.instance.label = tab.displayName;
component.instance.ref = component;
}
}