I'd like to get current header of the PrimeNG tabView. I tried to do it by binding activeIndex property but it didn't work.
Asked
Active
Viewed 8,107 times
2 Answers
8
You can get a reference to your tabView in your component using @ViewChild and bind to a variable the selectedIndex then you can get the header of selected tavview this.tabView.tabs[this.selectedIndex].header
.
Below a sample code: app.component.ts
import { Component, ViewChild } from '@angular/core';
//imports
import {TabView, TabPanel} from 'primeng/primeng';
@Component({
selector: 'my-app',
templateUrl: 'app.component.html'
})
export class AppComponent {
selectedIndex = 0;
//class variable
@ViewChild(TabView) tabView: TabView;
onChange($event) {
this.selectedIndex = $event.index;
}
getSelectedHeader(){
console.log(this.tabView.tabs[this.selectedIndex].header);
}
}
app.component.html
<h2>PrimeNG Issue Template</h2>
<button pButton (click)="getSelectedHeader()" label="selected header"></button>
<p-tabView (onChange)="onChange($event)" [activeIndex]="selectedIndex">
<p-tabPanel header="first" [selected]="true">
<first></first>
</p-tabPanel>
<p-tabPanel header="second" cache="false">
<ng-template pTemplate="content">
<second></second>
</ng-template>
</p-tabPanel>
</p-tabView>

GUISSOUMA Issam
- 2,572
- 2
- 16
- 27
-
Thank you so much! – AKZB Nov 13 '17 at 10:42
-
Exactly what I was looking for – Manubhargav May 14 '18 at 14:20
-
Thanks, this helped me too – Anna Sep 19 '18 at 07:08
-
Plunker code doesn't render tab component and gets stuck on Loading... – Shahzad Aug 23 '20 at 18:59
-
For those who facing issues with the `TabView` import, please change `import {TabView, TabPanel} from 'primeng/primeng';` to `import {TabView, TabPanel} from 'primeng/tabview';` – rtvalluri May 05 '23 at 05:14
3
.html
<p-tabView #tabView (onChange)="tabViewChange($event, tabView)">...</p-tabView>
.ts
tabViewChange(event, tabView: TabView) {
const headerValue = tabView.tabs[event.index].header;
}

faye.babacar78
- 774
- 7
- 12