Open In App

Angular PrimeNG TabMenu ActiveItem

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. This article will show us how to use the TabMenu ActiveItem in Angular PrimeNG.

The TabMenu component is used to make a navigation bar that will display the nav items as a nav header ie., it is a menu in the form of tabs. By default, the active route-matching item is highlighted; however, the activeItem property may be used to choose the initial active item.

Syntax:

<p-tabMenu 
        [model]="..." 
        [activeItem]="...">
</p-tabMenu>

Angular PrimeNG TabMenu ActiveItem properties:

  • model: It is an array of menu items. It accepts the array as an input data type & the default value is null.
  • activeItem: It defines the default active menu item. It accepts the menu item as an input 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: After the complete installation, it will look like the following:

 

Run the below command to see the output:

ng serve --open

Example 1: Below is the example that illustrates the use of Angular PrimeNG TabMenu ActiveItem.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>PrimeNG TabMenu TabMenu activeItem</h5>
      
    <p-tabMenu 
          [model]="gfg"
          [activeItem]="active">
    </p-tabMenu>
</div>


app.component.ts




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    gfg: MenuItem[];
    active: MenuItem;
  
    ngOnInit() {
        this.gfg = [
            {
                label: 'HTML',
            },
            {
                label: 'CSS',
            },
            {
                label: 'JavaScript',
            },
            {
                label: 'AngularJS',
            },
            {
                label: 'AngularJS',
            },
            {
                label: 'ReactJS',
            },
            {
                label: 'PrimeNG',
            },
        ];
        this.active = this.gfg[2];
    }
}


app.module.ts




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 { MessageModule } from 'primeng/message';
import { TabMenuModule } from 'primeng/tabmenu';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MessageModule,
        TabMenuModule,
        RouterModule.forRoot([
            {
                path: '',
                component: AppComponent
            }
        ]),
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

Example 2: Below is another example that illustrates the use of Angular PrimeNG TabMenu ActiveItem using the icons on the tab menu.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>PrimeNG TabMenu TabMenu activeItem</h5>
      
    <p-tabMenu 
          [model]="gfg"
          [activeItem]="active">
    </p-tabMenu>
</div>


app.component.ts




import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    gfg: MenuItem[];
    active: MenuItem;
  
    ngOnInit() {
        this.gfg = [
            {
                label: 'Whatsapp',
                icon: "pi pi-whatsapp"
            },
            {
                label: 'Linkedin',
                icon: "pi pi-linkedin"
            },
            {
                label: 'Instagram',
                icon: "pi pi-instagram"
            },
            {
                label: 'Slack',
                icon: "pi pi-slack"
            },
        ];
        this.active = this.gfg[1];
    }
}


app.module.ts




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 { MessageModule } from 'primeng/message';
import { TabMenuModule } from 'primeng/tabmenu';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MessageModule,
        TabMenuModule,
        RouterModule.forRoot([
            {
                path: '',
                component: AppComponent
            }
        ]),
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

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



Last Updated : 26 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads