Search results

Getting started with Angular Accordion component

06 Oct 2022 / 7 minutes to read

This section briefly explains about how to create a simple Angular Accordion and configure its available functionalities in Angular using Angular quickstart.

Dependencies

The following list of dependencies are required to use the Accordion component in your application.

Copied to clipboard
|-- @syncfusion/ej2-angular-navigations
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-navigations

Setup Angular Environment

You can use Angular CLI to setup your Angular applications. To install Angular CLI use the following command.

Copied to clipboard
npm install -g @angular/cli

Create an Angular Application

Start a new Angular application using below Angular CLI command.

Copied to clipboard
ng new my-app
cd my-app

Installing Syncfusion Accordion package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of 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.

Copied to clipboard
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.

Copied to clipboard
npm install @syncfusion/ej2-angular-navigations@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

Copied to clipboard
@syncfusion/ej2-angular-navigations:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Registering Accordion Module

Import Accordion module into Angular application(app.module.ts) from the package @syncfusion/ej2-angular-navigations [src/app/app.module.ts].

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

@NgModule({
  //declaration of ej2-angular-navigations module into NgModule
  imports:      [ BrowserModule, AccordionModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Adding CSS reference

The following CSS files are available in ../node_modules/@syncfusion package folder. This can be referenced in [src/styles.css] using following code.

Copied to clipboard
@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] file to render the accordion component. Add the Angular Accordion by using <ejs-accordion> selector in template section of the app.component.ts file.

Copied to clipboard
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  // specifies the template string for the Accordion component
  template: `<ejs-accordion> </ejs-accordion>`
})
export class AppComponent {

}

Initialize the Accordion using Items

The Accordion can be rendered by defining an array of items.

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

@Component({
selector: 'my-app',
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 {

}
  • Run the application in the browser using the following command.
Copied to clipboard
npm start

The following code example depicts the way to initialize the Accordion on a single element.

Output will be as follows:

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewChild } from '@angular/core';

@Component({
selector: 'app-container',
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 {

}
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);

Initialize the Accordion using HTML elements

The Accordion component can be rendered based on the given HTML element using <ejs-accordion>. You need to follow the below structure of HTML elements to render the Accordion inside the <ejs-accordion> tag.

Copied to clipboard
  <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>
Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewChild } from '@angular/core';

@Component({
selector: 'app-container',
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 {

}
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);

You can add the custom class into Accordion component using cssClass property which is used to customize the Accordion component.

See Also

N> You can refer to our Angular Accordion feature tour page for its groundbreaking feature representations. You can also explore our Angular Accordion example that shows you how to configure the Accordion in Angular.