13

We are developing a corporate component library which should provide Material Designed Angular Components. So the users of this library are not supposed to use e.g. Angular Material directly but rather include some component like "custom-tabs".

Using the components of MatTabModule directly works like a charm, whereas when using our custom components the projected content does not show up.

Usage looks very similar to the Angular Material API:

<custom-tabs>
  <custom-tab [label]="labelA">Content A</custom-tab>
  <custom-tab [label]="labelB">Content B</custom-tab>
  <custom-tab [label]="labelC">Content C</custom-tab>
</custom-tabs>

The custom components try to project the content as follows:

<!-- custom-tabs template -->
<mat-tab-group>
  <ng-content></ng-content>
</mat-tab-group>

<!-- custom-tab template -->
<mat-tab [label]="label">
  <ng-content></ng-content>
</mat-tab>

Does anyone have an idea how we can get it working?

I provided a StackBlitz where you can see the problem in action.

Robert Gruner
  • 133
  • 3
  • 7

2 Answers2

9

material has changed the var names, so in the solution

tabs -> _allTabs

tabGroup -> _tabBodyWrapper

Phil Baird
  • 91
  • 1
  • 1
5

I think the issue you have is the same described in this github issue: https://github.com/primefaces/primeng/issues/1215

Basically the problem here is that ng-content does not provide @ContentChild when crossing component boundaries.

You can see that mat-tab uses @ContentChild: https://github.com/angular/components/blob/master/src/material/tabs/tab.ts#L56

So I think the only solution is to override it programmatically the way it is described in primeng issue.

raphisama
  • 396
  • 3
  • 11
Dariusz Ostolski
  • 462
  • 3
  • 10
  • Thanks a lot for the link! Guess we will try the suggestion anytime soon. – Robert Gruner Mar 13 '18 at 17:49
  • 1
    Really got it working this way. Thanks a alot @dariusz-ostolski! I updated the [StackBlitz](https://angular-material2-custom-tabs.stackblitz.io) to show how it works. The trick is the AfterViewInit part in CustomTabsComponent. Don't know if this is the most elegant solution but it works for now. – Robert Gruner Jun 11 '18 at 16:06
  • I get the following error for this solution: No provider for InjectionToken MAT_TAB_GROUP! Anyone knows how to fix it? – Aleksa Mar 21 '22 at 16:09