Open In App

Angular PrimeNG TabView Events

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 TabView Events in Angular PrimeNG.

The TabView is a container component to group content with tabs. The events allow triggering different actions that take place in the application. The events fire a callback which is bonded to them.

Angular PrimeNG TabView Events:

  • onChange(event): It is the callback to invoke when a tab is changed.
  • onClose(event): It is the callback to invoke when a tab is closed.

 

Syntax:

<p-tabView (event-name)='...'>
</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 Events, where we are using onchange event.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Events</h4>
  
<p-tabView (onChange)="handleChange($event)">
    <p-tabPanel header="Coding">
        <ng-template pTemplate="content">
            <p>Code Here</p>
        </ng-template>
    </p-tabPanel>
    <p-tabPanel header="Web Technologies">
        <ng-template pTemplate="content">
            <p>Learn About Web Technologies</p>
        </ng-template>
    </p-tabPanel>
    <p-tabPanel header="Articles">
        <ng-template pTemplate="content">
            Read Some Technical Articles
        </ng-template>
    </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 {
    handleChange(e) {
        console.log('Index is :', e.index);
    }
    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 Events.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Events</h4>
  
<p-tabView (onClose)="handleChange($event)">
    <p-tabPanel header="Coding" [closable]="true">
        <ng-template pTemplate="content">
            <p>Code Here</p>
        </ng-template>
    </p-tabPanel>
    <p-tabPanel header="Web Technologies" [closable]="true">
        <ng-template pTemplate="content">
            <p>Learn About Web Technologies</p>
        </ng-template>
    </p-tabPanel>
    <p-tabPanel header="Articles" [closable]="true">
        <ng-template pTemplate="content">
            Read Some Technical Articles
        </ng-template>
    </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 {
    handleChange(e) {
        console.log('Tab Number', e.index, 'is Closed');
    }
    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
Share your thoughts in the comments

Similar Reads