Select an option in the Angular drop-down menu
We will cover how to set the selected option in an Angular dropdown, set the selected option from an array of options, and set the selected option if the options are dynamic in Angular.
Setting the selected option in an Angular dropdown menu
This article will discuss setting a selected option for an Angular dropdown menu. When you create a dropdown with multiple options, no option is automatically selected.
Therefore, we will first create a drop-down list and set the selected option and discuss different scenarios in the best way to set the selected option when the options are static or dynamic or when there is a range of options.
In app.component.html
, we will create a select form.
# angular
<select [(ngModel)]='ngDropdown' name="dropdownOption" id="dropdownOption" class='form-control'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
As you can see in this code, we use [(ngModel)]
, which creates an FormControl
instance of and binds it to a form control element.
We'll app.component.css
add some CSS to improve our drop-down form.
# angular
.form-control{
padding: 5px 10px;
background-color: rgb(52, 119, 179);
color: white;
border-color: black;
width: 100px;
height: 30px;
}
option:hover{
background-color: white;
color: rgb(52, 119, 179);
}
Output:
We can see that no option is selected, but when we click on it all the options appear.
To set the option to be checked, we can assign the value to app.component.ts
the variable in ngDropdown
. Looking at app.component.html
the code in , we see that we have assigned the variable [(ngModel)]='ngDropdown'
.
Let's try 1
setting to the default value.
# angular
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
ngDropdown = 1;
}
After assigning the selected option, we notice that the drop-down menu displays 1
as the selected option. This way, we can select any option as the selected option in Angular.
Output:
Sets the selected option from the options array
We will discuss how to set a default value from a set of options.
First, we'll create two variables: ngDropdown
first, the default option for our dropdown menu, and second ngOptions
, which will contain all the options we want to display in our dropdown.
So, let's app.component.ts
add this code inside .
# angular
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
ngOptions = [1,2,3,4,5,6,7,8,9,10]
ngDropdown = 1;
}
In ngOptions
the array, we set the values from 1
to 10
; in ngDropdown
, we 1
set to the default value.
We will app.component.html
create a selection form in .
# angular
<select class="form-control" id="DropdownOptions">
<option
*ngFor="let opt of ngOptions"
[selected]="opt === ngDropdown"
[value]="opt"
>
{{ opt }}
</option>
</select>
In this code, we will see that we [selected]
set to ngDropdown
, so when any option is ngDropdown
the same as , it will automatically be selected.
Output:
Option to select from dynamic values
We will discuss setting a selected option if we have dynamic values for the options. First, we will create two variables; the first one ngDropdown
, where we select the default option for the form, and the second one ngOptions
, which will contain all the values we want to display in the dropdown options.
We know there will be dynamic values, so let's have an array of random numbers and set the selected option for the second item in that array.
# angular
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
ngOptions = [3,6,1,4,2,10,7,5,9,8]
ngDropdown = this.ngOptions[1];
}
app.component.html
Will have the following code.
# angular
<select class="form-control" id="DropdownOptions">
<option
*ngFor="let opt of ngOptions"
[selected]="opt === ngDropdown"
[value]="opt"
>
{{ opt }}
</option>
</select>
Output:
So we can see that the second item of the array is set to the selected option. We can change it by changing the index of the array.
This tutorial taught us how to set the selected option of an Angular dropdown menu. We also discussed three different scenarios for selecting an option when the value is static, dynamic, or an array.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在 Angular 下拉菜单中选择选项
Publish Date:2024/03/24 Views:210 Category:Angular
-
本教程演示了如何在 Angular 下拉菜单中设置选定的选项。
在 Angular 中上传文件
Publish Date:2023/04/14 Views:123 Category:Angular
-
本教程演示了如何在 Angular 中上传任何文件。我们还将介绍如何在文件上传时显示进度条,并在上传完成时显示文件上传完成消息。
Angular 中所有 Mat 图标的列表
Publish Date:2023/04/14 Views:189 Category:Angular
-
本教程演示了在哪里可以找到 Angular 中所有 Mat 图标的列表以及如何使用它们。
Angular 2 中的复选框双向数据绑定
Publish Date:2023/04/14 Views:194 Category:Angular
-
本教程演示了如何一键标记两个复选框。这篇有 Angular 的文章将着眼于执行复选框双向数据绑定的不同方法。
在 AngularJS 中重新加载页面
Publish Date:2023/04/14 Views:205 Category:Angular
-
我们可以借助 windows.location.reload 和 reload 方法在 AngularJS 中重新加载页面。
在 AngularJs 中设置 Select From Typescript 的默认选项值
Publish Date:2023/04/14 Views:138 Category:Angular
-
本教程提供了在 AngularJs 中从 TypeScript 中设置 HTML 标记选择的默认选项的解释性解决方案。
在 AngularJS 中启用 HTML5 模式
Publish Date:2023/04/14 Views:190 Category:Angular
-
本文讨论如何在 AngularJS 应用程序上启用带有深度链接的 HTML5 模式。
在 AngularJs 中加载 spinner
Publish Date:2023/04/14 Views:127 Category:Angular
-
我们将介绍如何在请求加载时添加加载 spinner,并在 AngularJs 中加载数据时停止加载器。
在 Angular 中显示和隐藏
Publish Date:2023/04/14 Views:412 Category:Angular
-
本教程演示了 Angular 中的显示和隐藏。在开发商业应用程序时,我们需要根据用户角色或条件隐藏一些数据。我们必须根据该应用程序中的条件显示相同的数据。