Tabs

I'm using the Syncfusion ejs-tab component and attempting to treat each tab as a separate module. However, when I switch tabs — opening a new one and returning to a previously opened tab — the previously loaded component is re-initialized. Is there a way to fully maintain the state of the tab so that it doesn't reinitialize?

import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, PLATFORM_ID, ViewChild, AfterViewInit, Input, TemplateRef, ElementRef, ContentChildren, QueryList, ContentChild, ChangeDetectorRef, OnChanges } from '@angular/core';
import { TabComponent, TabModule } from '@syncfusion/ej2-angular-navigations';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-orb-frame-tab-menu',
  imports: [TabModule, CommonModule],
  templateUrl: 'orb-frame-tab-menu.component.html',
  styleUrls: ['orb-frame-tab-menu.component.scss']
})
export class OrbFrameTabMenuComponent implements OnChanges {
  @Input() aba1!: any;
  @Input() aba2!: any;
  @ViewChild('hiddenTab', { static: false }) tabObj!: TabComponent;
 
  public tabs: any[] = [];
  public headerText: any[] = [
    { 'text': 'Tabela' },
    { 'text': 'Formulario' }
  ];

  constructor(private cdr: ChangeDetectorRef) {}

  ngOnChanges() {
    this.cdr.detectChanges();
  }

  onTabCreated(event: any): void {
    const tabElement = document.getElementById('hiddenTab');

    if (tabElement) {
      // Remove listeners de touch/mouse que causam swipe
      tabElement.addEventListener('touchstart', (e) => e.stopImmediatePropagation(), true);
      tabElement.addEventListener('touchmove', (e) => e.stopImmediatePropagation(), true);
      tabElement.addEventListener('mousedown', (e) => e.stopImmediatePropagation(), true);
      tabElement.addEventListener('mousemove', (e) => e.stopImmediatePropagation(), true);
    }
  }

  // Adiciona uma nova aba dinamicamente
  addTab(tabTitle: string, tabContent: TemplateRef<any>, code: any) {
    const newTab = {
      header: { 'text': tabTitle },
      content: tabContent,
      index: code
    };
   
    this.headerText.push(newTab.header);
    this.tabs.push(newTab);
   
    // Retorna o índice da nova aba
    return this.headerText.length - 1;
  }

  // Remove uma aba pelo índice
  changeTab(code: any) {
    const tabIdx = this.tabs.findIndex(tab => tab.index === code);
    if (tabIdx !== -1 && this.tabObj) {
      this.tabObj.selectedItem = tabIdx;
    }
  }

  // Remove uma aba com base no "index" (code)
  removeTab(code: any) {
    const tabIdx = this.tabs.findIndex(tab => tab.index === code);
    if (tabIdx !== -1) {
      this.headerText.splice(tabIdx, 1);
      this.tabs.splice(tabIdx, 1);
      this.cdr.detectChanges();
    }
  }

  getOpenTabs(): any {
    return this.tabs;
  }
}

<ejs-tab #hiddenTab id="hiddenTab" cssClass="hidden-tab" [animation]='{ previous: { effect: "None" }, next: { effect: "None" } }' (created)="onTabCreated($event)">
  <e-tabitems>    
    <!-- Abas dinâmicas -->
    <e-tabitem *ngFor="let tab of tabs; let i = index" [header]='tab.header'>
      <ng-template #content>
        <div class="content-item">
          <ng-template [ngTemplateOutlet]="tab.content"></ng-template>
        </div>
      </ng-template>
    </e-tabitem>
  </e-tabitems>
</ejs-tab>

1 Reply

VR Vijay Ravi Syncfusion Team May 30, 2025 11:23 AM UTC

Hi Eric Hausman, 


We have checked and validated your reported query. When rendering different components inside the Tab component, please ensure that enablePersistence is set to true for each of the rendered components. This will ensure that the state of the last active component is properly maintained. Please refer to the sample shared below for your reference.

Sample link: https://stackblitz.com/edit/angular-pzujciqx-wvtwzo2z?file=src%2Fapp.component.html,src%2Fapp.component.ts

 
Don't hesitate to get in touch if you require further help or more information.

Regards,

Vijay


Loader.
Up arrow icon