在 Angular 下拉菜单中选择选项
我们将介绍如何在 Angular 下拉列表中设置选定选项,从选项数组中设置选定选项,以及如果选项在 Angular 中是动态的,则设置选定选项。
在 Angular 下拉菜单中设置选定选项
本文将讨论为 Angular 下拉菜单设置一个选定的选项。创建具有多个选项的下拉列表时,不会自动选择任何选项。
因此,我们将首先创建一个下拉列表并设置选定选项,并在选项静态或动态或有一系列选项时讨论设置选定选项的最佳方法中讨论不同的方案。
在 app.component.html
中,我们将创建一个选择表单。
# 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>
正如在这段代码中看到的,我们使用了 [(ngModel)]
,它创建了一个 FormControl
实例并将其绑定到一个表单控件元素。
我们将在 app.component.css
中添加一些 CSS 来改进我们的下拉表单。
# 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);
}
输出:
我们可以看到没有选择任何选项,但是单击它时会出现所有选项。
要将选项设置为选中,我们可以将值分配给 app.component.ts
中的变量 ngDropdown
。查看 app.component.html
中的代码,我们会看到我们已经分配了变量 [(ngModel)]='ngDropdown'
。
让我们尝试将 1
设置为默认值。
# angular
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
ngDropdown = 1;
}
分配选定选项后,我们注意到下拉菜单显示 1
作为选定选项。这样,我们可以选择任何选项作为 Angular 中的选定选项。
输出:
从选项数组中设置选定的选项
我们将讨论如何从一组选项中设置默认值。
首先,我们将创建两个变量:ngDropdown
,我们的下拉菜单的默认选项,第二个是 ngOptions
,它将包含我们想要在下拉选项中显示的所有选项。
所以,让我们在 app.component.ts
中添加这段代码。
# 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;
}
在 ngOptions
数组中,我们将值从 1
设置为 10
;在 ngDropdown
中,我们将 1
设置为默认值。
我们将在 app.component.html
中创建一个选择表单。
# angular
<select class="form-control" id="DropdownOptions">
<option
*ngFor="let opt of ngOptions"
[selected]="opt === ngDropdown"
[value]="opt"
>
{{ opt }}
</option>
</select>
在这段代码中,我们将看到我们将 [selected]
设置为 ngDropdown
,因此当任何选项与 ngDropdown
相同时,它将自动被选中。
输出:
从动态值中选择的选项
如果我们有选项的动态值,我们将讨论设置一个选定的选项。首先,我们将创建两个变量;第一个是 ngDropdown
,我们选择表单的默认选项,第二个是 ngOptions
,它将包含我们想要在下拉选项中显示的所有值。
我们知道会有动态值,所以让我们有一个随机数数组并为该数组中的第二项设置选定的选项。
# 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
将具有以下代码。
# angular
<select class="form-control" id="DropdownOptions">
<option
*ngFor="let opt of ngOptions"
[selected]="opt === ngDropdown"
[value]="opt"
>
{{ opt }}
</option>
</select>
输出:
因此,我们可以看到该数组的第二项设置为选定选项。我们可以通过改变数组的索引来改变它。
本教程教我们如何设置 Angular 下拉菜单的选定选项。我们还讨论了当值为静态、动态或数组时选择选项的三种不同场景。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。