Open In App

Angular PrimeNG TabView Programmatic Control

Last Updated : 07 Nov, 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 Programmatic Control in Angular PrimeNG.

The TabView Programmatic Control is used to control the tabs programmatically using the activeIndex property that defines the index of the active tab.

Angular PrimeNG TabView Programmatic Control Properties:

  • activeIndex: It is the index of the currently active tab to control the tab view programmatically.

 

Angular PrimeNG TabView Programmatic Control p-tabPanel Properties:

  • header: It is used to specify the header of the tab panel element.

Syntax:

// In app.component.ts
toggle() {
    return index;
}

// In app.component.html
<p-button (click)="index = toggle()" label="Toggle"></p-button>
  <br />
</div>
<p-tabView [(activeIndex)]="index">
</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:

Project Structure

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

  • app.component.html:

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Programmatic Control</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="Coding">
        <p>Code Here</p>
    </p-tabPanel>
    <p-tabPanel header="Web Technologies">
        <p>Learn About Web Technologies</p>
    </p-tabPanel>
    <p-tabPanel header="Articles">
        <p>Read some Tech Articles</p>
    </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() { }
}


  • 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 Programmatic Control, where we are toggling the tabs using the typescript function.

  • app.component.html

HTML




<h2 style="color: green">
  GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Tabview
    Programmatic Control
</h4>
<div style="padding: .5em 0 1em 0">
    <p-button (click)="index = toggle()" 
              label="Toggle">
    </p-button><br />
</div>
<p-tabView [(activeIndex)]="index">
    <p-tabPanel header="Coding">
        <p>Code Here</p>
    </p-tabPanel>
    <p-tabPanel header="Web Technologies">
        <p>Learn About Web Technologies</p>
    </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 {
    toggle() {
        return 1;
    }
    constructor() { }
    ngOnInit() { }
}


  • 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