简体   繁体   中英

Angular Tabs - Use only one component

I'm a beginner in angular and need to create some tabs. I found a very interesting example, but I am not able to implement everything in the same component and without the tab component template.

Is there any way to pass all the content from the component tab to the component app?

DEMO

I intend this code (tab component) to be transferred to the app component and all the html code in the template will be transferred to app.component.html. is it possible to implement this?

Thank you.

import { Component, Input } from '@angular/core';

@Component({
  selector: 'tabs',
  template: `
    <mat-tab-group>
      <ng-container *ngFor="let tab of tabsName">
        <mat-tab label="{{ tab.name }}">
          <ng-container [ngTemplateOutlet]='tab.template'></ng-container>
        </mat-tab>
      </ng-container>
    </mat-tab-group>
    <ng-content></ng-content>
  `,
  styles: [`h1 { font-family: Lato; }`]
})
export class TabComponent  {
  @Input() tabsName: any;

  onSelect(event){
    console.log(this.tabsName[0].name)
  }
}

If you wish to copy the entire content to app component, do so by replacing the input variable with a local variable.

App component template:

<mat-tab-group>
  <ng-container *ngFor="let tab of allTabs">
    <mat-tab label="{{ tab.name }}">
      <ng-container [ngTemplateOutlet]='tab.template'></ng-container>
    </mat-tab>
  </ng-container>
</mat-tab-group>
<ng-content></ng-content>

<ng-template #Shop>
  <form>
    name: <input type="text">
  </form>
</ng-template>
<ng-template #Notification>
  <h1>list of notification</h1>
</ng-template>
<ng-template #Review>
  <h1>list of review</h1>
</ng-template>

I've modified your Stackblitz to remove tab component.

Try to use more simple and flexible angular-tabs. https://www.npmjs.com/package/@codehint-ng/tabs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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