繁体   English   中英

将数据传递给 Angular 中的子组件数组

[英]Pass data to Array of Child Components in Angular

我的项目中有父组件和子组件,以及从父组件中的 API 调用的对象数组。

  • 父组件
public Questions = [];
ngOnInit(): void {
    this.loadQuestions();
}
<div *ngIf="Questions ">
   <app-child-card *ngFor="let item of Questions; index as i;" [data]="item" [index]="i" ></app-child-card>
</div>
  • 子组件
@Input() data: any;
@Input() index: any;
ngOnInit(): void {
    console.log(this.data, this.index);
}

即使我的问题数组为空,子组件也会在每次尝试中准确呈现三次,并在控制台中得到 output undefined undefined

空数组被接受为true条件,因此您需要将条件更改为*ngIf="Questions.length>0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM