Getting started with Angular Accordion component
11 Jul 202418 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.
To get start quickly with Angular Accordion, you can check on this video:
Dependencies
The following list of dependencies are required to use the Accordion component in your application.
|-- @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.
npm install -g @angular/cli
Create an Angular Application
Start a new Angular application using below Angular CLI command.
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,
- Ivy library distribution package format
- 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
To mention the ngcc package in the package.json
file, add the suffix -ngcc
with the package version as below.
@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.
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.
@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.
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 rendered by defining an array of items
.
import { AccordionModule } from '@syncfusion/ej2-angular-navigations'
import { Component } 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 { }
-
Run the application in the browser using the following command.
npm start
The following code example depicts the way to initialize the Accordion on a single element.
Output will be as follows:
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
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.
<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 add the custom class into Accordion component using
cssClass
property which is used to customize the Accordion component.
See Also
NOTE
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.