Getting started with Angular Combo box component
25 Jul 202412 minutes to read
This section explains how to create a simple ComboBox component and configure its available functionalities in Angular.
To get started quickly with angular ComboBox component using angular CLI, you can check the video below.
Dependencies
The following list of dependencies are required to use the Angular ComboBox component in your application.
|-- @syncfusion/ej2-angular-dropdowns
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-angular-base
|-- @syncfusion/ej2-dropdowns
|-- @syncfusion/ej2-lists
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-buttons
Setup angular environment
Angular provides the easiest way to set angular CLI projects using Angular CLI
tool.
Install the CLI application globally to your machine.
npm install -g @angular/cli
Create a new application
ng new syncfusion-angular-combobox
By default, it install the CSS style base application. To setup with SCSS, pass –style=scss argument on create project.
Example code snippet.
ng new syncfusion-angular-combobox --style=scss
Navigate to the created project folder.
cd syncfusion-angular-combobox
Installing Syncfusion ComboBox 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-dropdowns
package to the application.
npm install @syncfusion/ej2-angular-dropdowns --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-dropdowns@ngcc
package to the application.
npm install @syncfusion/ej2-angular-dropdowns@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-dropdowns:"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.
Registering ComboBox module
Import ComboBox module into Angular application(app.module.ts) from the package @syncfusion/ej2-angular-dropdowns
.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the ComboBoxModule for the ComboBox component
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns';
import { AppComponent } from './app.component';
@NgModule({
//declaration of ej2-angular-dropdowns module into NgModule
imports: [ BrowserModule, ComboBoxModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
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-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-dropdowns/styles/material.css';
Adding ComboBox component
Modify the template in [src/app/app.component.ts] file to render the Angular ComboBox component. Add the Angular ComboBox by using <ejs-combobox>
selector in template
section of the app.component.ts file.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component
template: `<ejs-combobox id='comboelement'></ejs-combobox>`
})
export class AppComponent { }
Binding data source
After initializing, populate the ComboBox with data using the dataSource
property. Here, an array of string values passed to ComboBox component.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
// specifies the template string for the ComboBox component
template: `<ejs-combobox id='comboelement' [dataSource]='data'></ejs-combobox>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: string[] = ['Cricket', 'Football', 'Rugby', 'Snooker', 'Tennis'];
}
Running the application
After completing the configuration required to render a basic ComboBox, run the following command to display the output in your default browser.
ng serve
The following example illustrates the output in your browser.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the ComboBox component with dataSource
template: `<ejs-combobox id='comboelement' [dataSource]='data' placeholder = 'Select a game'></ejs-combobox>`
})
export class AppComponent {
constructor() {
}
// defined the array of data
public data: string[] = ['Cricket', 'Football', 'Rugby', 'Snooker', 'Tennis'];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Custom values
The ComboBox allows the user to give input as custom value which is not required to present in predefined set of values. By default, this support is enabled by allowCustom property. In this case, both text field and value field considered as same. The custom value will be sent to post back handler when a form is about to be submitted.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the ComboBox component with dataSource
template: `<ejs-combobox id='comboelement' [dataSource]='sportsData' [fields]=fields allowCustom=true placeholder = 'Select a game'></ejs-combobox>`
})
export class AppComponent {
constructor() {
}
public fields: Object = {text: 'Game', value: 'Id'};
// defined the array of data
public sportsData: { [key: string]: Object }[] = [
{ Id: 'game1', Game: 'Badminton' },
{ Id: 'game2', Game: 'Football' },
{ Id: 'game3', Game: 'Tennis' }
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Configure the popup list
By default, the width of the popup list automatically adjusts according to the ComboBox input element’s width, and the height of the popup list has ‘300px’.
The height and width of the popup list can also be customized using the popupHeight and popupWidth property respectively.
In the following sample, popup list’s width and height are configured.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the ComboBox component with value property
template: `<ejs-combobox id='comboelement' #samples [dataSource]='data' placeholder='Select a game' popupHeight='200px' popupWidth='250px'></ejs-combobox>`
})
export class AppComponent {
constructor() {
}
// define the array of data
public data: string[] = ['Cricket', 'Football', 'Golf', 'Rugby', 'Snooker', 'Tennis'];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Two-way binding
In ComboBox, the value
property supports two-way binding functionality. The following example demonstrates how to work the two-way binding functionality in ComboBox.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ComboBoxModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, ComboBoxModule,ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the ComboBox component and
// input element for checking the two-way binding support using value property
template: `
<ejs-combobox id='comboelement' [dataSource]='data' [(value)]='value' placeholder="Select a game"></ejs-combobox>
<div style='margin-top: 50px'>
<input type="text" [(ngModel)]="value" style='width:245px;height:25px' />
</div>
`
})
export class AppComponent {
constructor() {
}
// defined the array of complex data
public data: string[] = [ 'Badminton', 'Football', 'Rugby', 'Snooker', 'Tennis' ];
// set a value to pre-select
public value: string = 'Badminton';
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
You can refer to our Angular ComboBox feature tour page for its groundbreaking feature representations. You can also explore our Angular ComboBox example that shows how to render the ComboBox in Angular.