How to set a Selected Option of a Dropdown List control using Angular ?
Last Updated :
02 Apr, 2024
In Angular, a select or dropdown list is a common UI component that allows users to choose a single value from a predefined set of options. Angular select allows us to bind the dropdown list to a data source, such as an array or an object, using directives like `*ngFor`. It allows 2-way data binding using [(ngModel)] directive. Angular also provides us with Event Handling functions like (change), (selectionChange), etc, which allows us to handle events triggered by the dropdown list such as changing the selected option.
In this article, we will see how to set a selected option of a dropdown list control.
Approach 1. Set a selected option using [(ngModel)]
ngModel directive is used for two-way data binding. Here we are using ngModel to establish the two-way data binding between the variable of our component class and the selected option in the dropdown.
Syntax:
<select [(ngModel)]="selectedValue">
<option value="option1">Option 1 </option>
<option value="option2">Option 2 </option>
<option value="option3">Option 3 </option>
</select>
Here, we are binding `selectedValue` variable using ngModel , so we can update the selectedValue variable in our component file and set the dropdown list. Let us look into an example using this approach :
Example :
HTML
<!-- app.component.html -->
<select [(ngModel)]="color">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="black">Black</option>
<option value="white">White</option>
</select>
<p>Selected Color : {{ color }}</p>
JavaScript
//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
color: string = 'red';
}
Output:

Here we gave the default value as 'red' for variable `color` which we used for 2 way data binding using ngModel. Hence the coolor red is selected by default in the dropdown. Since it is a two-way data binding, whenever there is change of option selected, it is updating the color variable as well.
Approach 2. Set a selected option using [selected] property
Here we will be using [selected] property binding on each option, to set the selected option based on the condition. Here we directly set the selected attribute on each option element based on a comparison with the selectedValue property in our component. This approach is simpler and only requires setting the initial selected value.
Syntax :
<select>
<option [selected]="selectedValue==='option1'" value="option1">Option 1 </option>
<option [selected]="selectedValue==='option2'" value="option2">Option 2 </option>
<option [selected]="selectedValue==='option3'" value="option3">Option 3 </option>
</select>
This approach directly sets the selected value in the dropdown listt based on the value given for `selectedValue` in our component class. Lets look at an example:
Example:
HTML
<!-- app.component.html -->
<div>
<select>
<option [selected]="color === 'red'" value="red">Red</option>
<option [selected]="color === 'green'" value="green">Green</option>
<option [selected]="color === 'black'" value="black">Black</option>
<option [selected]="color === 'white'" value="white">White</option>
</select>
<p>Selected Color : {{ color }}</p>
</div>
JavaScript
//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
color: string = 'green';
}
Output:

Here we are using [selected] property binding to each option, with the condition which compares color variable to value of each option. We gave the value of color variable to green, so it automatically sets the default value of drop down list to green , as this is not the two-way binding, even though we are changing the selected option, it is not updating the variable , hence we can only see `Selected Color : green` .
Similar Reads
How to make a multi-select dropdown using Angular 11/10 ?
In this article, we will learn to build the multiple selection drop-down menu in Angular. To accomplish this task, we require Angular 10 or the Angular 11 version. Sometimes we need to display dynamically fetched multi-selected data in a drop-down menu, for this, we will use the npm @ng-select/ng-se
3 min read
How to change a select's options based on another dropdown using React?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front end library responsible only for the view layer of the application. It is maintained by Facebook.Select in HTML allows us to choose among
3 min read
How to change Selected Value of a Drop-Down List using jQuery?
We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
3 min read
How to choose multiple options from dropdown as a tag using ReactJS?
Choosing multiple options from the dropdown means allowing the user to choose more than one option from the dropdown. Material UI for React has this component available for us and it is very easy to integrate. We can choose multiple options from the dropdown using the following approach. Creating Re
2 min read
How to create a To-Do list using Drag and Drop in Angular 7 ?
We can easily create a To-Do list using Drag-Drop module provided by angular Component Development Kit (CDK). First of all, create an angular component by using the following command- ng g c To-Do Now import CdkDragDrop, moveItemInArray, transferArrayItem from @angular/cdk/drag-drop to our to-Do com
2 min read
How To Create A Cascading Dropdown List Using JavaScript?
The Cascading dropdown lists are used to display options in a secondary dropdown based on the selection made in a primary dropdown. This is useful in forms where users need to select categories or locations in the hierarchical manner. For example: selecting a country might reveal states or provinces
3 min read
How to use ngfor to make a dropdown in Angular from an array ?
In this post, we will see how we can display the array elements in the drop-down menu using AngularJS. Sometimes we need to display dynamically fetched data and this is where features ngFor come into the scene. We can iterate over the array, apply conditions and display the data easily.Using ngFor:
2 min read
How to make Dropdown using Angular UI Bootstrap ?
In this article, we will see how to make Dropdown using Angular UI bootstrap. Angular UI Bootstrap is an Angular JS framework created by Angular UI developers for providing better UI which can be used easily. Syntax: <div uib-dropdown></div> Download AngularUI from the link: https://angu
2 min read
How to Add Validator to FormControl After Control is Created in Angular ?
Adding a validator to a form typically refers to the process of defining rules or conditions that suggest whether the data entered into the form fields is valid or not. Initially, the form can be submitted without any validation. However, after adding a validation rule, submission is only allowed if
4 min read
Angular PrimeNG Form Dropdown Disabled Options Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use the calendar component in angular ngx bootstrap. Dropdown Disab
3 min read