We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get started easily with Syncfusion Angular 11 Dropdown List?

Platform: Angular - EJ 2 |
Control: DropDownList

A quick start project that helps you to create an Angular 11 Drop Down List with a minimal code configuration.

The following section explains the steps required to create a simple Angular 11 Drop Down List component.

 

Project Pre-requisites

 

Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.

Introduction

 

The Angular 11 DropDown list used in this project is created from the Syncfusion ej2-angular-dropdowns package. You can simply define it as  <ejs-dropdownlist> within the template.

Dependencies

 

Before starting with this project, the Angular 11 DropDown list requires to add the Syncfusion ej2-angular-dropdowns packages from npmjs, which are distributed in npm as @syncfusion scoped packages.

 

Creating Angular Project

 

To create the Angular project using the Angular CLI tool, follow the steps.

 

  1. Install the Angular CLI application in your machine.

 

  1. npm install -g @angular/cli@11.2.3

 

 

Note:

 

If you would like to follow and run the application in Angular6 or Angular5 or Angular4, you need to replace the CLI command version number with the corresponding angular version number.

 

npm install -g @angular/cli@<CLI VERSION>

 

 

  1. Now create a new Angular project by using the command ng new and navigate to that folder.

 

  1. ng new angular11-app
  2. cd angular11-app

 

  1. Install the ej2-angular-dropdowns package through the npm install command.

 

  1. npm install @syncfusion/ej2-angular-dropdowns --save
  2. npm install @syncfusion/ej2 save

 

Adding Angular 11 Dropdown List

 

You can add the Angular 11 Dropdown List component by using the ejs-dropdownlist directive. The attributes used within this tag allow you to define other Dropdown List functionalities. To add the Angular 11 Dropdown List, follow the steps:

  1. Import the DropDownListModule into the app.module.ts file from the ej2-angular- dropdowns package.
  2. Import and inject the other required modules within the providers section of app.module.ts.

[app.module.ts]

import { BrowserModule, } from '@angular/platform-browser';

import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns’;

import { NgModule } from '@angular/core';

import { FormsModule }   from '@angular/forms';

import { AppComponent } from './app.component';

 

@NgModule({

  declarations: [

    AppComponent

  ],

  imports: [

    BrowserModule, FormsModule, DropDownListModule

  ],

  bootstrap: [AppComponent]

})

export class AppModule { }

 

  1. Define the Angular Dropdown List code within the app.component.html file which is mapped against the templateUrl option in the app.component.ts file.

[app.component.html]

<ejs-dropdownlist></ejs-dropdownlist>

 

  1. Refer to the CDN link of CSS reference within the index.html file.

 

<link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet" />

 

  1. Run the application with the ng serve command, and an empty Dropdown List will be displayed on the browser. Now, you can load the Dropdown List with data.

Screenshot

Shape, rectangle

Description automatically generated with medium confidence

 

Loading Data

 

You can populate the empty Dropdown List with data by using the JSON data through the dataSource property.

[app.component.ts]

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

     // define the JSON of data

     public countries: { [key: string]: Object; }[] = [

            { Name: 'Australia', Code: 'AU' },

            { Name: 'Bermuda', Code: 'BM' },

            { Name: 'Canada', Code: 'CA' },

            { Name: 'Cameroon', Code: 'CM' },

            { Name: 'Denmark', Code: 'DK' },

            { Name: 'France', Code: 'FR' },

            { Name: 'Finland', Code: 'FI' },

            { Name: 'Germany', Code: 'DE' }, 

        ];

        // maps the local data column to fields property

        public localFields: Object = { text: 'Name', value: 'Code' };

        // set the placeholder to Dropdown List input element

        public localWaterMark: string = 'Select countries';

}

 

[app.component.html]

 

<ejs-dropdownlist id='localData' #local [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark'></ejs-dropdownlist >

 

ScreenShot

Graphical user interface, application

Description automatically generated

 

Setting initial select value on Dropdown List

 

You can populate the empty Dropdown List with value by binding the string data through the value property initially.

[app.component.ts]

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

         public value: string = 'AU';

}

 

Now, assign this value to the value property of Angular Dropdown List within the app.component.html file.

[app.component.html]

<ejs-dropdownlist id='localData' #local [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark' [value]='value'

></ejs-dropdownlist >

 

Screenshot

Shape

Description automatically generated

 

Getting the selected value on form submit

 

By using the ngModel, you can get the value from the ngform submit.

[app.component.ts]

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

        onSubmit(form: NgForm): void {

            console.log(form.value.name);

          }

}

 

[app.component.html]

<form #form='ngForm' (ngSubmit)="onSubmit(form)">

    <div class="form-group"> 

        <ejs-dropdownlist id='localData' name='name'  #local='ngModel' [(value)]='value'[(ngModel)]='value' [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark'></ejs-dropdownlist>

        <button type="submit" ejs-button>Submit</button>

    </div>

  </form>    

 

Run the application with the command ng serve in the command prompt.

You can view the Angular Dropdown List output with the data and other settings.

Screenshot

 

Graphical user interface, application

Description automatically generated

There are more options to explore with UG Documentation and you can also try to play with the downloadable example link in this knowledge base article.

 

2X faster development

The ultimate Angular UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile