angular - Angular: If this option in dropdown show this div
问题描述
How do you show and hide divs in Angular (4 thru 9) upon a user selecting an option(s) in a dropdown select?
My code below is only showing one/the same div no matter the option I select.
public Terminated = true;
public Release = true;
selectedFilter:string;
public filterTypes = [
{value:'empty', display:''},
{value:'release', display:'Release Report'},
{value:'reports', display:'Detail Report'},
{value:'terminated', display:'Terminated Report'},
];
constructor() {
this.selectedFilter='release';
}
filterChanged(selectedValue:string){
if(this.selectedFilter) {
this.Terminated = false;
} else {
this.Release = false
}
<select class="form-control" (change)="filterChanged($event.target.value)">
<option *ngFor="let type of filterTypes" [value]="type.value">{{type.display}}
</option>
</select>
<div [hidden]="Terminated"><p>terminated content here</p></div>
<div [hidden]="Release"><p>release content here</p></div>
解决方案
In your code, you are checking selectedFilter
to set the variables to true and false, but the value of this variable is only set in the constructor. Due to which the change in the select drop down is not having any affect.
You may change your code to the below,
component.html
<select class="form-control" [(ngModel)]="selectedFilter">
<option *ngFor="let type of filterTypes" [value]="type.value">
{{type.display}}
</option>
</select>
<div [hidden]="selectedFilter !== 'terminated'"><p>terminated content here</p></div>
<div [hidden]="selectedFilter !== 'release'"><p>release content here</p></div>
<div [hidden]="selectedFilter !== 'reports'"><p>detailed report content here</p></div>
component.ts
selectedFilter:string;
public filterTypes = [
{value:'empty', display:''},
{value:'release', display:'Release Report'},
{value:'reports', display:'Detail Report'},
{value:'terminated', display:'Terminated Report'},
];
constructor() {
this.selectedFilter = 'release';
}
推荐阅读
- maven - Maven:根据星期几修改工件版本
- php - 从 laravel 会话中清除数组(laravel 5.2)
- plc - B&R Automation Studio 转移发布活动
- c# - EF 不首先识别代码的列属性?
- c# - 始终在 Visual Studio 中运行自定义生成事件(不使用 VC++)
- python - 将多个表数据从 SQL Server 迁移到 Oracle
- android - 检查网址是否为带有扩展名限制的图片
- bash - 如何使用 sed 在 mac 终端中查找和替换字符串?
- algorithm - Clusterization algorithm
- javascript - Firebase 无法使用电子邮件和密码注册用户