Background
The ComboBox control allows users to select an item from a dropdown list. To perform a custom action when a user clicks the control, attach a native JavaScript click event listener to its hostElement.
Use the ComboBox’s initialized event to access the control and register the listener after initialization.
Steps to Complete
- Import the ComboBox module and define the component.
- Register the click listener during initialization.
- Create the ComboBox in the template.
Getting Started
Import the ComboBox module and define the component
import { Component, OnDestroy } from '@angular/core';
import { WjInputModule } from '@mescius/wijmo.angular2.input';
import { ComboBox } from '@mescius/wijmo.input';
@Component({
selector: 'app-root',
standalone: true,
imports: [WjInputModule],
templateUrl: './app.component.html'
})
export class AppComponent implements OnDestroy {
countries = ['US', 'Germany', 'UK', 'Japan'];
private comboBoxHost: HTMLElement | null = null;
private handleClick = (): void => {
console.log('ComboBox clicked');
};
initializeComboBox(comboBox: ComboBox): void {
this.removeClickListener();
this.comboBoxHost = comboBox.hostElement;
this.comboBoxHost.addEventListener(
'click',
this.handleClick,
true
);
}
ngOnDestroy(): void {
this.removeClickListener();
}
private removeClickListener(): void {
this.comboBoxHost?.removeEventListener(
'click',
this.handleClick,
true
);
this.comboBoxHost = null;
}
}
WjInputModuleprovides the Angular ComboBox component.- The
countriesarray supplies the dropdown items.
Register the click listener during initialization
this.comboBoxHost = comboBox.hostElement;
this.comboBoxHost.addEventListener(
'click',
this.handleClick,
true
);
- The
hostElementproperty provides access to the DOM element that hosts the ComboBox. The third argument,true, registers the listener for the capture phase, allowing it to receive clicks as they travel toward elements inside the control. - The
handleClickfunction logs a message to the browser console. Replace this message with the action your application needs to perform. - The component removes the listener in
ngOnDestroy, using the same handler and capture setting supplied during registration.
Create the ComboBox in the template
<wj-combo-box
#comboBox
[itemsSource]="countries"
(initialized)="initializeComboBox(comboBox)">
</wj-combo-box>
- The
itemsSourceproperty binds the control to thecountriesarray. The template reference variable,comboBox, passes the control instance toinitializeComboBoxwhen theinitializedevent fires. - This listener handles clicks within the ComboBox’s host element, including its input and dropdown button. It does not indicate that the selected item changed. Use
selectedIndexChangedwhen your action should respond to a selection change.
With this setup, the ComboBox responds to clicks through a native JavaScript event listener attached to its host element, allowing you to perform custom actions when users interact with the control.
Happy coding!
Andrew Peterson
Technical Engagement Engineer