How to get selected value of dropdown in Angular?

Dropdowns are a common feature in web applications, allowing users to select options from a list. In Angular, getting the selected value of a dropdown is a straightforward process that involves using a combination of HTML and TypeScript code. In this article, we will explore the steps required to achieve this and provide answers to some commonly asked questions regarding this topic.

How to get the selected value of a dropdown in Angular?

To obtain the selected value of a dropdown in Angular, you can make use of the “ngModel” directive provided by Angular Forms. Here is a step-by-step guide:

1. Start by creating a dropdown element in your HTML template using the <select> tag.
2. Use the [(ngModel)] directive to bind the selected value to a variable in your component.
3. Assign each option in the dropdown a value using the [value] property.
4. In your TypeScript component, define a variable to hold the selected dropdown value.
5. Now, whenever the user selects an option from the dropdown, the selected value will be automatically updated in the variable.

Here is an example to illustrate the process:


<select [(ngModel)]="selectedValue">
<option [value]="'option1'">Option 1</option>
<option [value]="'option2'">Option 2</option>
<option [value]="'option3'">Option 3</option>
</select>

In the above example, the selected value will be assigned to the variable named “selectedValue” in the component.

How to access the selected value in Angular?

To access the selected value in Angular, you can simply refer to the variable to which you have bound the “ngModel” directive. In the case of our previous example, you can access the selected value using the “selectedValue” variable.

What is the value of the selected option if no value is explicitly defined?

If you do not define a value explicitly for an option, the value will be set to the inner text of the, the value will be “Example”.

Can I use ngModel with two-way data binding for the selected value?

Yes, you can use two-way data binding with ngModel, which allows you to both receive and update the selected value of the dropdown.

Can I set an initial value for the dropdown?

Yes, you can set an initial value for the dropdown by assigning the desired value to the variable bound in ngModel in your component.

How can I set a default option to display in the dropdown?

To display a default option in the dropdown, you can add an

How to get the selected option’s text instead of its value?

To get the selected option’s text, you can make use of Angular’s ViewChild decorator to access the