0

我的项目中有父组件和子组件,以及从父组件中的 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);
}

即使我的问题数组是空的,子组件也会在每次尝试中准确呈现 3 次,并undefined undefined在控制台中得到输出。

4

1 回答 1

4

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

于 2020-08-16T04:51:24.737 回答