gpt4 book ai didi

Angular:如果下拉列表中的此选项显示此 div

转载 作者:行者123 更新时间:2023-12-04 09:35:10 25 4
gpt4 key购买 nike

当用户在下拉选择中选择一个选项时,您如何在 Angular(4 到 9)中显示和隐藏 div?

无论我选择什么选项,我下面的代码只显示一个/相同的 div。

 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>

最佳答案

在您的代码中,您正在检查 selectedFilter 以将变量设置为 true 和 false,但此变量的值仅在构造函数中设置。因此,选择下拉列表中的更改没有任何影响。

您可以将代码更改为以下,

组件.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>

组件.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';
}

关于Angular:如果下拉列表中的此选项显示此 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62625182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com