Search results

Load content through Ajax in Angular Accordion component

27 Oct 2021 / 1 minute to read

Accordion supports to load external contents through AJAX library. Refer the below steps.

  • Import the Ajax module from ej2-base and initialize with URL path.
  • Get data from the Ajax Success event to initialize Accordion with retrieved external path data.
Source
Preview
app.component.ts
app.module.ts
main.ts
ajax.html
Copied to clipboard
import { Component, ViewChild } from '@angular/core';
import { Ajax } from '@syncfusion/ej2-base';
import { AccordionComponent} from '@syncfusion/ej2-angular-navigations';

@Component({
selector: 'app-container',
template: `
<div id="acrdnContnet1" style="display:none">
    <ul style="margin : 0px;padding:0px 16px; list-style-type: none">
      <li>Testing</li>
      <li>Development</li>
    </ul>
</div>
<div id="acrdnContnet2" style="display:none">
  <ul style="margin : 0px;padding:0px 16px; list-style-type: none">
    <li>Mobile</li>
    <li>Web</li>
  </ul>
</div>
<ejs-accordion #acrdnInstance>
  <e-accordionitems>
    <e-accordionitem header='Department' content = '#acrdnContnet1'></e-accordionitem>
    <e-accordionitem header='Platform' content = '#acrdnContnet2'></e-accordionitem>
    <e-accordionitem header='Employee Details'></e-accordionitem>
  </e-accordionitems>
</ejs-accordion>
    `
})

export class AppComponent {
@ViewChild('acrdnInstance') acrdnInstance: AccordionComponent;
public contentData: string;

ngOnInit() {
let ajax: Ajax = new Ajax('./ajax.html', 'GET', true);
ajax.send().then();
ajax.onSuccess = (data: string): void => {
   this.acrdnInstance.items[2].content = data;
   this.acrdnInstance.refresh();
};
}
}}
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);
Copied to clipboard
<div>
    <div class="content">
      <img src="https://ej2.syncfusion.com/demos/src/schedule/images/alice.png" alt="alice_img" />
      <br>
      <div id="headername">
        <b>Alice</b>
      </div><br><br> Alice is a software engineer. He develops the web components. He is passionate about web technologies.
    </div>
</div>