Having trouble getting help?
Contact Support
Contact Support
Value change in Angular Drop down list component
27 Apr 20244 minutes to read
You can check about whether value change happened by manual or programmatic by
using change
event argument that argument name is isInteracted
.
The following example demonstrate, how to check whether value change happened by manual or programmatic.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component , ViewChild} from '@angular/core';
import { DropDownListComponent } from '@syncfusion/ej2-angular-dropdowns';
@Component({
imports: [
FormsModule, DropDownListModule
],
standalone: true,
selector: 'app-root',
// specifies the template path for DropDownList component
templateUrl: `template.html`
})
export class AppComponent {
constructor() {
}
ngAfterViewInit(){
document.getElementById('btn')!.onclick = () => {
this.dropDownListObject!.value = 'game3';
}
}
// defined the array of data
public data: { [key: string]: Object }[] = [ { Id: 'game1', Game: 'Badminton' },
{ Id: 'game2', Game: 'Football' }, { Id: 'game3', Game: 'Tennis' }];
// maps the appropriate column to fields property
public fields: Object = { text: 'Game', value: 'Id' };
//set the placeholder to DropDownList input
public text: string = "Select a game";
@ViewChild('ddlelement')
public dropDownListObject?: DropDownListComponent;
onChange(args: any): void {
let element: HTMLElement = document.createElement('p');
if (args.isInteracted) {
element.innerText = 'Changes happened by Interaction';
} else {
element.innerText = 'Changes happened by programmatic';
}
document.getElementById('event')!.append(element);
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
<div class="control-section">
<div class="content-wrapper">
<div id='icon'>
<div class="content" style="margin: 50px auto 0; width:250px;">
<ejs-dropdownlist id='ddlelement' #ddlelement [dataSource]='data' [fields]='fields' [placeholder]='text' (change)='onChange($event)'></ejs-dropdownlist>
<div style='padding: 50px 0'>
<button id='btn' ej-button #btn class="e-control e-btn"> Set Value Dynamically </button>
</div>
<p id='event'></p>
</div>
</div>
</div>
</div>