我正在尝试使用角度为7的字符串键调用组件。因为我的服务为我提供了每个用户要在页面上显示哪些组件筛选器的键。我可以特别在html中做这个过滤器吗?
<pg-tab *ngFor="let tab of tabs; let index = index">
<ng-template #TabHeading>
{{tab.headerText}}
<a (click)="removeTab(index)" style="padding: 5px;"><i class="fa fa-times fa-lg" style="color:orangered;"></i></a>
</ng-template>
<div class="row column-seperation" *ngIf="tab.componentName === 'grid-sample'">
<app-grid-sample></app-grid-sample>
</div>
<div class="row column-seperation" *ngIf="tab.componentName === 'pdf-export-sample'">
<app-pdf-export-sample></app-pdf-export-sample>
</div>
<div class="row column-seperation" *ngIf="tab.componentName === 'notify-sample'">
<app-notify-sample></app-notify-sample>
</div>
<div class="row column-seperation" *ngIf="tab.componentName === 'loader-sample'">
<app-loader-sample></app-loader-sample>
</div>
<div class="row column-seperation" *ngIf="tab.componentName === 'tt-form-elements'">
<app-tt-form-elements></app-tt-form-elements>
</div>
<div class="row column-seperation" *ngIf="tab.componentName === 'tt-layouts'">
<app-tt-layouts></app-tt-layouts>
</div>
</pg-tab>
我搜索了innerHtml属性,但不适用于该示例中的角度组件。
html
<div [innerHTML]="pageDetail"></div>
打字稿
private _pageDetail: string = '<app-tab-page1 [tab]="tab" [tabsLength]="tabs.length" [tabs]="tabs"></app-tab-page1><button>Naber</button>';
public get pageDetail(): SafeHtml {
return this._sanitizer.bypassSecurityTrustHtml(this._pageDetail);
}
您可以猜测它现在看起来不太好。如果可能的话,我想变短并清理html中的“ * ngIf”属性。
您对此有什么想法吗?
类似这样的东西
<div class="row column-seperation" tab.componentName>
<app-tab.componentName></app-tab.componentName>
</div>
注意:很抱歉我的语法错误。
答案 0 :(得分:1)
您可以使用以下名称创建动态组件:
标签项是指令:
@ViewChildren('tabItem', {read: ViewContainerRef} ) tabItem: QueryList<ViewContainerRef>;
在这里创建动态组件:
const factories = Array.from(this.componentFactoryResolver['_factories'].keys());
const factoryClass = <Type<any>>factories.find((x: any) => x.name === dynamicComponentName);
if (factoryClass !== undefined) {
const factory = this.componentFactoryResolver.resolveComponentFactory(factoryClass);
const ref = this.tabItem.viewContainerRef;
ref.createComponent(factory);
}
您还可以查看角度文档:dynamic component
答案 1 :(得分:0)
有两种方法:
ngSwitch
对减少html代码无济于事。ngComponentOutlet
可以从父组件代码而不是模板中提供要显示的组件。第2个示例:
模板:
<div>
... some other code
<ng-template matTabContent>
<ng-container *ngComponentOutlet="screen.component; injector: screen.injector">
</ng-container>
</ng-template>
</div>
父组件代码:
export class TabsContainerComponent implements OnInit {
// make sure these component are also added to entryComponents
private components = [
GridSampleComponent,
NotifySampleComponent,
LoaderSampleComponent,
TTLayoutComponent,
...
];
screen: any; // reference to be used by the ngComponentOutlet in the template
constructor(
private injector: Injector,
) {}
async ngOnInit() {
const tabs = await this.someApiService.getComponentFilters().toPromise();
const injector = Injector.create({
providers: [any providers you want to pass to the component],
parent: this.injector
})
switch (tabs.componentName) {
case 'grid-sample':
screen = { component: GridSampleComponent, injector };
break;
case 'notify-sample':
screen = { component: NotifySampleComponent, injector };
break;
case 'loader-sample':
screen = { component: LoaderSampleComponent, injector };
break;
case 'tt-layout':
screen = { component: TTLayoutComponent, injector };
break;
}
}
}
答案 2 :(得分:0)
您可以简单地使用ngSwitch来使其更短,但是动态组件名称是根据我所知无法实现的,
<div class="row column-seperation" *ngSwitch="tab.componentName">
<app-grid-sample *ngSwitchCase="'grid-sample'"></app-grid-sample>
<app-pdf-export-sample *ngSwitchCase="'pdf-export-sample''"></app-pdf-export-sample>
//etc ...
</div>