我正在try 显示注册总数为item.male_studentsitem.female_students

UPDATE
移除ngFor现在它可以按预期工作. 这是我的组件.html

<thead class="thead-zircon">
    <tr>
        <th>
            Total Enrollment Quick Count
        </th>
    </tr>
</thead>
<tbody class="list">
    <tr>
        <td>
            {{ maleCount + femaleCount }}
        </td>
    </tr>
</tbody>

UPDATED
这是我的配料.

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);
    });
  }

我有异议了.以下是来自API端点的和图像

enter image description here

UPDATE
但我得到了所有的索引.我只想在表中显示男女学生的总数

enter image description here

推荐答案

您可以在收到来自AJAX的数据后立即执行此操作.在这种情况下,这是我建议你的好办法.

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);
   });
}

超文本标记语言

Male Count - {{ maleCount }}
Female Count - {{ femaleCount }}

在您的情况下,如果从user修改为data,并且我们希望保持UI绑定的更新.那么在绑定上使用Pipefunction调用肯定会是一个更好的主意.

@Pipe({name: 'countByProp'})
export class CountByPropPipe implements PipeTransform {
  transform(data: any, prop: string): number {
    if (!prop) throw new Error('Please pass property name');
    return data.reduce((acc, item) => acc + item[prop]), 0);
  }
}

超文本标记语言

Male Count - {{ data | countByProp: 'male_students' }}
Female Count - {{ data | countByProp: 'female_students' }}

TS

在您的HomePage.ts中定义maleCountfemaleCount变量

maleCount = 0;
femaleCount = 0;

Angular相关问答推荐

如何go 除融合图中的拖尾水印?

元件不工作时的Angular 17属性 Select 器

Angular 客户端应用程序无法从停靠容器解析后端服务的地址

带有服务依赖的Angular测试类

Rxjs重置执行后的可观察延迟并重新调度Angular

如何将Angular 服务包含在延迟加载的独立组件路由中?

无法在Mat-SideNav中绑定模式

RXJS拆分返回值,多次调用后重新加入

找不到模块webpack dev服务器

对象中的值丢失 angular ngrx

大型 Angular 12 应用程序 - 我如何管理 js 文件

多个 Mat Paginator 在 Angular 组件中不起作用

使用 rxjs 处理刷新令牌

Angular2 + Jspm.io:使用类decorator 时需要反射元数据垫片

ERROR 错误:StaticInjectorError(AppModule)[UserformService -> HttpClient]:

NG 测试中的调试测试

Angular 2+ ngModule 中导入/导出的作用

如何在功能模块层次 struct 中使用 .forRoot()

angular2 测试,我如何模拟子组件

如何从指令访问主机组件?