Getting started with Angular Accordion component

22 Sep 202516 minutes to read

This section explains how to create a simple Angular Accordion and configure its core functionalities, such as expanding and collapsing items, in Angular using Angular CLI. The Accordion component is ideal for organizing content into collapsible sections, supporting single or multiple item expansion.

To get started quickly with the Angular Accordion, watch this video demonstrating its basic setup and configuration:

Dependencies

The following dependencies are required to use the Accordion component in your Angular application:

|-- @syncfusion/ej2-angular-navigations
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-navigations

Setup Angular Environment

Use Angular CLI to set up your Angular application. Install Angular CLI with the following command:

npm install -g @angular/cli

Create an Angular Application

Create a new Angular application using the following Angular CLI command:

ng new my-app
cd my-app

Installing Syncfusion® Accordion package

Syncfusion® packages are available on npm as @syncfusion scoped packages. Find all Angular Syncfusion® packages at npm.

Syncfusion® provides two package structures for Angular components:

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion® Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-navigations package to the application.

npm install @syncfusion/ej2-angular-navigations --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion® Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-navigations@ngcc package to the application.

npm install @syncfusion/ej2-angular-navigations@ngcc --save

In the package.json file, specify the ngcc package version with the -ngcc suffix:

@syncfusion/ej2-angular-navigations:"20.2.38-ngcc"

Note: Without the -ngcc suffix, the Ivy package is installed by default, which may cause warnings for Angular versions below 12.

Adding CSS reference

Include the following CSS files from the ../node_modules/@syncfusion folder in [src/styles.css]:

@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';

Add Accordion component

Modify the template in [src/app/app.component.ts] to render the Accordion component using the <ejs-accordion> selector:

import { AccordionModule } from '@syncfusion/ej2-angular-navigations'
import { Component } from '@angular/core';

@Component({
imports: [
  AccordionModule
],
standalone: true,
selector: 'app-root',
  // specifies the template string for the Accordion component
  template: ` <ejs-accordion>
        <e-accordionitems>
          <e-accordionitem expanded='true'>
            <ng-template #header>
              <div>ASP.NET</div>
            </ng-template>
            <ng-template #content>
              <div>Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web
                services. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop
                or mobile browser. ASP.NET pages use a compiled,event-driven programming model that improves performance and enables
                the separation of application logic and user interface.</div>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>ASP.NET MVC</div>
            </ng-template>
            <ng-template #content>
              <div>The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model,
                the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for
                creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that
                (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based
                authentication.
              </div>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>JavaScript</div>
            </ng-template>
            <ng-template #content>
              <div>JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers
                so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter
                the document content that was displayed.More recently, however, it has become common in both game development and
                the creation of desktop applications.</div>
            </ng-template>
          </e-accordionitem>
        </e-accordionitems>
      </ejs-accordion>`
})
export class AppComponent { }

Initialize the Accordion using Items

The Accordion can be populated using an array of items property for dynamic data. The following example demonstrates initialization with items:

import { AccordionModule } from '@syncfusion/ej2-angular-navigations';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [AccordionModule],
  template: `
    <ejs-accordion [items]="accordionItems"></ejs-accordion>
  `,
})
export class AppComponent {
  public accordionItems = [
    {
      header: 'ASP.NET',
      content: `Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop or mobile browser. ASP.NET pages use a compiled, event-driven programming model that improves performance and enables the separation of application logic and user interface.`,
      expanded: true,
    },
    {
      header: 'ASP.NET MVC',
      content: `The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. It is lightweight, highly testable, and integrates with existing ASP.NET features like master pages and membership-based authentication.`,
    },
    {
      header: 'JavaScript',
      content: `JavaScript (JS) is an interpreted programming language originally implemented in web browsers to enable client-side scripting. It allows interaction with users, asynchronous communication, and dynamic content updates. Today, JavaScript is widely used in game development and desktop application creation as well.`,
    },
  ];
}
  • Run the application in the browser using the following command.
npm start

The following example shows how to initialize the Accordion:

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccordionModule } from '@syncfusion/ej2-angular-navigations'



import { Component, ViewChild } from '@angular/core';

@Component({
imports: [
         AccordionModule
    ],


standalone: true,
    selector: 'app-root',
    template: `
    <ejs-accordion>
        <e-accordionitems>
          <e-accordionitem expanded='true'>
            <ng-template #header>
              <div>ASP.NET</div>
            </ng-template>
            <ng-template #content>
              <div>Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web
                services. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop
                or mobile browser. ASP.NET pages use a compiled,event-driven programming model that improves performance and enables
                the separation of application logic and user interface.</div>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>ASP.NET MVC</div>
            </ng-template>
            <ng-template #content>
              <div>The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model,
                the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for
                creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that
                (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based
                authentication.
              </div>
            </ng-template>
          </e-accordionitem>
          <e-accordionitem>
            <ng-template #header>
              <div>JavaScript</div>
            </ng-template>
            <ng-template #content>
              <div>JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers
                so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter
                the document content that was displayed.More recently, however, it has become common in both game development and
                the creation of desktop applications.</div>
            </ng-template>
          </e-accordionitem>
        </e-accordionitems>
    </ejs-accordion>
        `
})

export class AppComponent {

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Initialize the Accordion using HTML elements

Alternatively, the Accordion can be rendered using HTML elements within the <ejs-accordion> tag. Follow this structure:

  <ejs-accordion>   <!-- Root Accordion Element -->
       <div>      <!-- Accordion Item Container -->
            <div>   <!-- Accordion Header Container -->
                <div> </div> <!-- Accordion Header -->
            </div>
            <div>  <!-- Accordion Panel Container -->
                <div> </div> <!-- Accordion Content -->
             </div>
        </div>
  </ejs-accordion>
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { AccordionModule } from '@syncfusion/ej2-angular-navigations'



import { Component, ViewChild } from '@angular/core';

@Component({
imports: [
         AccordionModule
    ],


standalone: true,
    selector: 'app-root',
    template: `<ejs-accordion>
    <div>
        <div>
            <div> ASP.NET </div>
        </div>
        <div>
            <div> Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications
                  and XML Web services </div>
        </div>
    </div>
    <div>
        <div>
            <div> ASP.NET MVC </div>
        </div>
        <div>
            <div>The Model-View-Controller (MVC) architectural pattern separates an application into three main components:
                 the model, the view, and the controller </div>
        </div>
    </div>
    <div>
        <div>
            <div> JavaScript </div>
        </div>
        <div>
            <div>JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part
                 of web browsers so that client-side scripts could interact with the user, control the browser </div>
        </div>
    </div>
</ejs-accordion>`
})

export class AppComponent {

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

You can customize the Accordion’s appearance using the cssClass property.

See Also

Refer to the Angular Accordion feature tour page for its groundbreaking features or explore the Angular Accordion example to see how to configure the Accordion in Angular.