javascript - 使用下拉列表中的属性值过滤对象数组 - Angular 模板
问题描述
我正在尝试使用属性值过滤对象数组并在下拉列表中显示过滤结果。这是我的 JSON 结构:
var profiles = [{
"name":"Pavan",
"exp": 2,
"subject":"maths"
},
{
"name":"Mark",
"exp": 6,
"subject":"science"
},
{
"name":"sunny",
"exp": 1,
"subject":"maths"
},
{
"name":"Roy",
"exp": 2,
"subject":"science"
}]
在这里,我想在主题为数学的下拉列表中显示名称。我可以在 ts 文件中进行过滤,并且可以在下拉列表中显示,但我想自己在模板中进行过滤部分。现在我用下面的代码显示所有的名字。
<select name="profile" class="bx--text-input" [formControl]="profile">
<option value="" selected>Select profile name</option>
<option [value]="state.name" *ngFor="let state of profiles">{{state.name}}</option>
</select>
因此,如果我只想显示数学主题,如何在模板文件中进行过滤。
解决方案
<select name="profile" class="bx--text-input" [formControl]="profile">
<option value="" selected>Select profile name</option>
<ng-container *ngFor="let state of profiles;">
<option [value]="state.name" *ngIf="state.subject =='maths'">
{{ state.name }}
</option>
</ng-container>
</select>
推荐阅读
- regex - 需要一些帮助才能使正则表达式正常工作
- java - 如何在 Spring Boot mvc Web 应用程序中进行 bean 注入
- r - R:burnin.phybreak - if (runif(1) < exp(logaccprob)) 中的错误
- c# - MVC Kendo 可编辑网格不保存枚举值
- c# - 为什么 CreateTableClient 不存在?
- c# - 如何在 xamarin 中为文本设置动画?
- asp.net - 为什么我的 ASP.NET 网站项目针对 .NET Framework 4.6.1,而我已经告诉它以 4.8 为目标?
- javascript - 尝试在 JSON 中的两个项目上设置不同的样式
- python - 如何修改数据框中新创建的列的多个行范围?
- sql - 获取值的总和与条件匹配的行