Open In App

Angular PrimeNG TabView Programmatic

Last Updated : 18 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 know how to use the TabView Programmatic in Angular PrimeNG. We will also learn about the properties, events, and styling along with their syntaxes that will be used in the code.

The TabView Component is used to display content in the form of tabs.

Syntax:

<p-button 
    (click)="..." 
    label="...">
</p-button>

<p-tabView [(activeIndex)]="...">
    <p-tabPanel header="...">
        ...
    </p-tabPanel>
</p-tabView>

 

Angular PrimeNG TabView Programmatic Properties:

  • header: It specifies the title of the tabPanel. It is of string data type & the default value is null.
  • selected: It defines if the tab is active. It is of boolean data type & the default value is false.
  • activeIndex: It specifies the index of the active tab to change the selected tab programmatically. It accepts the number data type & the default value is null.

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:

 

  • Run the below command to see the output.
ng serve --open

Example 1: This is the basic example that illustrates how to use the Angular PrimeNG TabView Programmatic.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angualar PrimeNG TabView Programmatic</h5>
  
<div class="card">
    <div>
        <p-button 
            (click)="gfg = 0
            label="Activate Geek 1">
        </p-button>
        <p-button 
            (click)="gfg = 1
            label="Activate Geek 2">
        </p-button>
        <p-button 
            (click)="gfg = 2
            label="Activate Geek 3">
        </p-button>
    </div>
  
    <p-tabView [(activeIndex)]="gfg">
        <p-tabPanel header="Geek I">
            <p>Welcome Geek 1</p>
        </p-tabPanel>
        <p-tabPanel header="Geek II">
            <p>Welcome Geek 2</p>
        </p-tabPanel>
        <p-tabPanel header="Geek III">
            <p>Welcome Geek 3</p>
        </p-tabPanel>
    </p-tabView>
</div>


  • app.component.ts:

Javascript




import { 
    Component, 
    OnInit, 
    ViewEncapsulation } 
from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss'],
})
export class AppComponent {}


  • 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: This is another example that illustrates how to use the Angular PrimeNG TabView Programmatic using images.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angualar PrimeNG TabView Programmatic</h5>
  
<div class="card">
    <div>
        <p-button 
            (click)="gfg = 0
            label="Activate Course 1">
        </p-button>
        <p-button 
            (click)="gfg = 1
            label="Activate Course 2">
        </p-button>
        <p-button 
            (click)="gfg = 2
            label="Activate Course 3">
        </p-button>
    </div>
  
    <p-tabView [(activeIndex)]="gfg">
        <p-tabPanel header="Course 1">
            <p>
              <img alt="gfg" src=
            </p>
          </p-tabPanel>
          <p-tabPanel header="Course 2">
            <p>
              <img alt="gfg" src=
            </p>
          </p-tabPanel>
          <p-tabPanel header="Course 3" [selected]="true">
            <p>
              <img alt="gfg" src=
            </p>
          </p-tabPanel>
    </p-tabView>
</div>


  • app.component.ts:

Javascript




import { 
    Component, 
    OnInit, 
    ViewEncapsulation } 
from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss'],
})
export class AppComponent {}


  • 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
Share your thoughts in the comments

Similar Reads