简体   繁体   English

Angular Material 选项卡中动态加载的内容

[英]Dynamically loaded content in Angular Material Tabs

I'm trying to build a dynamic tab-system from Angular Material: Tabs and I'm running into an issue with the loading of content on subsequent tabs, where the concept only works on the first tab being loaded.我正在尝试从 Angular Material: Tabs 构建一个动态选项卡系统,但我遇到了在后续选项卡上加载内容的问题,该概念仅适用于加载的第一个选项卡。

Below is the tab-group.下面是选项卡组。 The tabs are constructed from an array of 'tabs' sent from some service, on the group, I have a change event, which should, when changed, dynamically load a component for the active tab in the .这些选项卡是由从某个服务发送的一组“选项卡”构成的,在组上,我有一个更改事件,该事件应该在更改时动态加载 .

<mat-tab-group *ngIf="tabs.length >= 1" #shiftplanningtabgroup (selectedTabChange)="selectedTabChange($event);">
    <mat-tab *ngFor="let tab of tabs; let idx = index;" [disabled]="tab.disabled">
        <ng-template mat-tab-label>
            <div class="ava-mat-tab-label font-size-12" fxLayout="row" fxLayoutGap="12px" fxLayoutAlign="center end">
                <mat-checkbox [checked]="tab.active" [disabled]="tab.disabled" (change)="checkboxStateChange($event, idx);"></mat-checkbox>
                <label class="cursor-pointer">{{ tab.label }}</label>
            </div>
        </ng-template>
        <div class="tab-content">
            <ng-template #tabHost></ng-template>
        </div>
    </mat-tab>
</mat-tab-group>

The 'selectedTabChange' event handler: 'selectedTabChange' 事件处理程序:

public selectedTabChange(event: MatTabChangeEvent): void {
  this.loadTabContent(event.index);
}

The 'loadTabContent' method: 'loadTabContent' 方法:

private loadTabContent(index: number): void {

  this.container.clear();

  const factory = this.componentFactoryResolver.resolveComponentFactory(AvaShiftPlanningTabContentComponent);

  const componentRef = this.container.createComponent<AvaShiftPlanningTabContentComponent>(factory);
  (componentRef.instance as AvaShiftPlanningTabContentComponent).loading = true;
  (componentRef.instance as AvaShiftPlanningTabContentComponent).tab = this.tabs[index];
  (componentRef.instance as AvaShiftPlanningTabContentComponent).tabLoadingCompleted.subscribe((value: any) => {
    this.tabLoadingComplete(value);
  });
}

And the reference to #tabHost:以及对#tabHost 的引用:

@ViewChild('tabHost', { read: ViewContainerRef }) container: ViewContainerRef;

This works, only on the first tab.这有效,仅在第一个选项卡上。 If I add more than one tab, all subsequent tabs show no content, however, the component is definitely being hit on tab change, just that the content for the dynamically loaded component is not rendered.如果我添加多个选项卡,所有后续选项卡都不会显示任何内容,但是,该组件肯定会在选项卡更改时被点击,只是不会呈现动态加载的组件的内容。

Is this a limitation on Material: Tabs or something wrong with the way that I'm trying to load the component?这是 Material: Tabs 的限制还是我尝试加载组件的方式有问题?

Hey I think this answer is late:嘿,我认为这个答案来晚了:

but I passed here so I thought if I gave help would be nice.但我经过这里,所以我想如果我提供帮助会很好。 this is how I do it.这就是我的做法。

         <mat-tab-group (selectedIndexChange)="selected.setValue($event)"
                       [selectedIndex]="selected.value" animationDuration="0"
                       class="h-100 w-100">
            <mat-tab *ngFor="let tab of tabs; let index = index">
                <ng-template class="w-100" mat-tab-label>
                    {{ tab.label }}
                    <mat-icon (click)="removeTab(index)" class="float-end">cancel</mat-icon>
                </ng-template>
                <ng-template #frame style="max-width: 100%;"></ng-template> // <-- notice here
            </mat-tab>
        </mat-tab-group>

The frame must be ViewChildren not ViewChild coz it has many items so you can't override the same viewContainerRef this is how you can use it.框架必须是 ViewChildren 而不是 ViewChild 因为它有很多项目,所以你不能覆盖相同的 viewContainerRef 这是你可以使用它的方式。

@ViewChildren('frame', { read: ViewContainerRef }) frame: QueryList<ViewContainerRef>;

The adding will be like this:添加将是这样的:

    this.tabs.push(action);

    if (selectAfterAdding) {
        this.selected.setValue(this.tabs.length - 1);
    }

and for the content dynamic you can use the Component Factory Resolver which you can find more here .对于内容动态,您可以使用组件工厂解析器,您可以在此处找到更多信息

Thanks and i hope this help anyone needed it.谢谢,我希望这可以帮助任何需要它的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM