Search results

Set the nested Accordion in Angular Accordion component

14 Apr 2021 / 2 minutes to read

You can render Accordion components inside the parent Accordion content using Angular ng-template. Through this, we can add content as Accordion components directly with all their functionalities to our Accordion. We need to use ng-template inside the each e-accordionitem tag with #content attribute, which is mandatory to render content. And now use ng-template tag with select attribute of id or class name for mapping required content.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewChild } from '@angular/core';
import { AccordionComponent } from '@syncfusion/ej2-angular-navigations';
import { ExpandEventArgs, Accordion, AccordionClickArgs} from '@syncfusion/ej2-navigations';

@Component({
    selector: 'app-container',
    template: `
      <ejs-accordion>
        <e-accordionitems>
          <e-accordionitem expanded="true">
            <ng-template #header>
              <div>Video</div>
            </ng-template>
            <ng-template #content>
              <ejs-accordion>
                <e-accordionitems>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Video Track 1</div>
                    </ng-template>
                  </e-accordionitem>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Video Track 2</div>
                    </ng-template>
                  </e-accordionitem>
                </e-accordionitems>
              </ejs-accordion>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>Music</div>
            </ng-template>
            <ng-template #content>
              <ejs-accordion>
                <e-accordionitems>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Music Track 1</div>
                    </ng-template>
                  </e-accordionitem>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Music Track 2</div>
                    </ng-template>
                  </e-accordionitem>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Music New</div>
                    </ng-template>
                    <ng-template #content>
                      <ejs-accordion>
                        <e-accordionitems>
                          <e-accordionitem>
                            <ng-template #header>
                              <div>New Track 1</div>
                            </ng-template>
                          </e-accordionitem>
                          <e-accordionitem>
                            <ng-template #header>
                              <div>New Track 2</div>
                            </ng-template>
                          </e-accordionitem>
                        </e-accordionitems>
                      </ejs-accordion>
                    </ng-template>
                  </e-accordionitem>
                </e-accordionitems>
              </ejs-accordion>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>Images</div>
            </ng-template>
            <ng-template #content>
              <ejs-accordion>
                <e-accordionitems>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Track 1</div>
                    </ng-template>
                  </e-accordionitem>
                  <e-accordionitem>
                    <ng-template #header>
                      <div>Track 2</div>
                    </ng-template>
                  </e-accordionitem>
                </e-accordionitems>
              </ejs-accordion>
            </ng-template>
          </e-accordionitem>
        </e-accordionitems>
      </ejs-accordion>
      `
})

export class AppComponent {
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AccordionModule } from '@syncfusion/ej2-angular-navigations';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, AccordionModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);