Open In App

Angular PrimeNG TabView Dynamic Tabs

Last Updated : 25 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the TabView Dynamic Tabs in Angular PrimeNG.

Angular PrimeNG facilitates the TabView component that acts as a container, in order to group the content with the tab. Dynamic Tabs is used to render tabs dynamically, the p-tabPanel components can be generated dynamically using the ngFor directive. For this, the selected property must be manually enabled.

Angular PrimeNG TabView Dynamic Tabs Attributes:

  • [header]: It represents the title of the tabPanel. It is of string type & its default value is null.
  • [selected]: It is defined only when the tab is active or the current tab is selected. It is of boolean type & its default value is false.

 

Syntax:

<p-tabView>
   <p-tabPanel [header]="item.header" 
                  *ngFor="let item of items; let i = index" 
                  [selected]="i == 0">
          {{item.content}}
   </p-tabPanel>
</p-tabView>

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG TabView Dynamic tabs.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Dynamic Tabs</h4>
  
<p-tabView [(activeIndex)]="index">
    <p-tabPanel [header]="item.header" 
                *ngFor="let item of items; 
                        let i = index
                [selected]="i == 0">
        {{ item.content }}
    </p-tabPanel>
</p-tabView>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
  
    ngOnInit() { }
    items = [
        { header: 'Coding', content: 'Code Here' },
        { header: 'Web Technologies'
          content: 'Learn About Web Technologies' },
        { header: 'Articles'
          content: 'Read some Tech Articles' },
    ];
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TabViewModule,
        ButtonModule,
        RouterModule.forRoot([
            { path: '', component: AppComponent }
  
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG TabView Dynamic tabs, where we are rendering dynamic tabs along with the use of programmatic control.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Dynamic Tabs</h4>
<div style="padding: .5em 0 1em 0">
    <p-button (click)="index = 0
              label="Activate Coding Tab">
    </p-button>
    <br /><br />
  
    <p-button (click)="index = 1
              label="Activate Web Technologies Tab">
    </p-button>
    <br /><br />
  
    <p-button (click)="index = 2
               label="Activate Articles Tab">
    </p-button>
</div>
<p-tabView [(activeIndex)]="index">
    <p-tabPanel [header]="item.header" 
                *ngFor="let item of items; 
                        let i = index
                [selected]="i == 0">
        {{ item.content }}
    </p-tabPanel>
</p-tabView>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [],
})
export class AppComponent {
    constructor() { }
  
    ngOnInit() { }
    items = [
        { header: 'Coding', content: 'Code Here' },
        { header: 'Web Technologies'
          content: 'Learn About Web Technologies' },
        { header: 'Articles'
          content: 'Read some Tech Articles' },
    ];
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TabViewModule,
        ButtonModule,
        RouterModule.forRoot([
            { path: '', component: AppComponent }
  
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/tabview



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads