angular5 - 如何重置下拉菜单并使其选择默认选项?
问题描述
我有多个下拉菜单,如下所示。第二个菜单的列表取决于第一个,依此类推。
<select [(ngModel)]="firstModel" id="first" (ngModelChange)="ChangeDropdown(this.wholeList[firstModel],'second')">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of first" [value]="item">{{item}}</option>
</select>
<br>
<select [(ngModel)]="secondModel" id="second" (ngModelChange)="ChangeDropdown(this.wholeList[firstModel][secondModel],'third')">
<option value="" disabled selected>select a category</option>
<option *ngFor="let item of second" [value]="item">{{item}}</option>
</select>
<br>
我需要的是让我们说如果用户从第二个菜单中选择数据,那么第二个菜单之后的菜单应该选择“选择类别”选项
我试过以下代码:
ChangeDropdown = (value,dropdownName) => {
if(dropdownName == 'second') {
this.secondModel = null;
this.thirdModel = null;
this.fourModel = null;
}
这不是选择选择类别选项,而是使菜单空白。
解决方案
在角垫选择
在 Ts 文件中
import {Component} from '@angular/core';
@Component({
selector: 'select-value-binding-example',
templateUrl: 'select-value-binding-example.html',
styleUrls: ['select-value-binding-example.css'],
})
export class SelectValueBindingExample {
firstModel = '';
secondModel = '';
thirdModel = '';
ChangeDropdown (eventInfo) {
console.log(eventInfo)
if(eventInfo.source._id == 'second') {
this.secondModel = '';
this.thirdModel = '';
}
}
}
在 HTML 文件中
<mat-form-field>
<mat-select id="first" [(ngModel)]="firstModel" (selectionChange)="ChangeDropdown($event)">
<mat-option value="" disabled>Select categor</mat-option>
<mat-option value="option1">Option 1</mat-option>
<mat-option value="option2">Option 2</mat-option>
<mat-option value="option3">Option 3</mat-option>
</mat-select>
</mat-form-field>
<br/>
<mat-form-field>
<mat-select id="second" [(ngModel)]="secondModel" (selectionChange)="ChangeDropdown($event)">
<mat-option value="" disabled>Select category</mat-option>
<mat-option value="option1">Option 1</mat-option>
<mat-option value="option2">Option 2</mat-option>
<mat-option value="option3">Option 3</mat-option>
</mat-select>
</mat-form-field>
<br/>
<mat-form-field>
<mat-select id="third" [(ngModel)]="thirdModel" (selectionChange)="ChangeDropdown($event)">
<mat-option value="" disabled>Select category</mat-option>
<mat-option value="option1">Option 1</mat-option>
<mat-option value="option2">Option 2</mat-option>
<mat-option value="option3">Option 3</mat-option>
</mat-select>
</mat-form-field>
推荐阅读
- purescript - 为来自 Halogen 的查询/请求获取“为表达式推断出无限类型”
- python-3.x - 如何在windows上运行QT平台插件
- linux - 为 Python 命名 make altinstall
- npm - “Cypress Run”的路径问题
- elasticsearch - 在 Elasticsearch 中的长文本中搜索带有空格的字符串
- php - 如果 PHP 中存在数组,则跳过添加到数组
- php - 如何在我的递归函数php中传递一个数组
- javascript - mongo 将 maxDistance 设置为集合中的值
- c - 如何唤醒暂停的线程?
- html - 如何制作填充物?