I’m trying to filter array of objects by using property value and display filter results in dropdown. This is my JSON structure:
JavaScript
x
20
20
1
var profiles = [{
2
"name":"Pavan",
3
"exp": 2,
4
"subject":"maths"
5
},
6
{
7
"name":"Mark",
8
"exp": 6,
9
"subject":"science"
10
},
11
{
12
"name":"sunny",
13
"exp": 1,
14
"subject":"maths"
15
},
16
{
17
"name":"Roy",
18
"exp": 2,
19
"subject":"science"
20
}]
Here I want to display names in dropdown where subject is maths. I can able to do filter in ts file and can able to display in dropdown, but I would like to do filter part in template it self. For now I’m displaying all names with below code.
JavaScript
1
4
1
<select name="profile" class="bx--text-input" [formControl]="profile">
2
<option value="" selected>Select profile name</option>
3
<option [value]="state.name" *ngFor="let state of profiles">{{state.name}}</option>
4
</select>
So if I want to display only subject of maths, how can i filter in template file.
Advertisement
Answer
JavaScript
1
8
1
<select name="profile" class="bx--text-input" [formControl]="profile">
2
<option value="" selected>Select profile name</option>
3
<ng-container *ngFor="let state of profiles;">
4
<option [value]="state.name" *ngIf="state.subject =='maths'">
5
{{ state.name }}
6
</option>
7
</ng-container>
8
</select>