Clear item in Angular Drop down list component
27 Apr 20242 minutes to read
You can clear the selected item in the below two different ways.
By clicking on the clear icon
which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction. By using showClearButton
property, you can enable the clear icon in DropDownList element.
Through programmatic you can set null
value to anyone of the index, text or value property to clear the selected item in DropDownList.
The following example demonstrate about how to clear the selected item in DropDownList.
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';
import { Button } from '@syncfusion/ej2-buttons';
@Component({
imports: [
FormsModule, DropDownListModule
],
standalone: true,
selector: 'control-content',
// specifies the template string for the DropDownList component with change event
templateUrl: `clear.html`
})
export class AppComponent {
constructor() {
}
ngAfterViewInit() {
// Set null value to value property for clear the selected item
document.getElementById('btn')!.onclick = () => {
(this.dropDownListObject as any).value = null;
}
}
// defined the array of data
public data: string[] = ['Badminton', 'Basketball', 'Cricket', 'Golf', 'Hockey', 'Rugby'];
// set placeholder text to DropDownList input element
public placeholder: string = 'Select a game';
@ViewChild('ddlelement')
public dropDownListObject?: DropDownListComponent;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));