Im trying to display total counts of enrollment with item.male_students
& item.female_students
UPDATE
remove the ngFor now it works as expected. here’s my component.html
<thead class="thead-zircon">
<tr>
<th>
Total Enrollment Quick Count
</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td>
{{ item.male_students + item.female_students }}
</td>
</tr>
</tbody>
UPDATED
here’s my component.ts
data: [];
constructor(private apiService: ApiService) {
super();
}
ngOnInit(): void {
this.getEnrollMentCountBySex();
}
getEnrollMentCountBySex() {
this.apiService.getAll('enrollments', "/count-student-enrollment-by-sex-and-academic-level")
.subscribe(data => {
this.data = data;
this.maleCount = data.reduce(((acc, item) => acc + item.male_students), 0);
this.femaleCount = data.reduce(((acc, item) => acc + item.female_students), 0);
});
}
Im getting object. Here’s and image from api endpoint
UPDATE
But im getting all the index. I only want the total count of male & female students to be shown in table
You can do it as soon as you receive data from ajax. That’s the good way I would suggest you in this case.
HTML
If
data
is being modified fromuser
in your case and we wanted to keep UI bindings updated. Then definitely usingPipe
orfunction
call on binding would be a better idea.HTML
TS
Define
maleCount
andfemaleCount
variable in yourHomePage.ts