简体   繁体   English

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

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

I have parent component and child components in my project and array of objects called from API in parent component.我的项目中有父组件和子组件,以及从父组件中的 API 调用的对象数组。

  • Parent Component父组件
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>
  • Child Component子组件
@Input() data: any;
@Input() index: any;
ngOnInit(): void {
    console.log(this.data, this.index);
}

Even if my questions array is empty, child component got rendered exact three times in each try and got output undefined undefined in console.即使我的问题数组为空,子组件也会在每次尝试中准确呈现三次,并在控制台中得到 output undefined undefined

empty array is accepted as true condition, so you need to change your condition to *ngIf="Questions.length>0"空数组被接受为true条件,因此您需要将条件更改为*ngIf="Questions.length>0"

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

相关问题 在子组件之间传递数组 Angular/TypeScript - Pass Array Between Child Components Angular/TypeScript 将解析器数据传递给子组件:Angular - Pass resolver data to child components : Angular 推入子组件时数组不更新跟随数据父传递给子组件 in Angular 行为 - Array when push in child components not update follow data parent pass into child in Angular Behavior Angular 9 - 如何将文本数据数组从父组件传递到多层子组件? - Angular 9 - How to pass array of text data from parent component to multiple layers of child components? 在 Angular 的嵌套子组件中传递数据的最佳方法是什么? - What could be the best approach to pass data in nested child components, in Angular? 如何将异步数据传递给对象中的子组件(Angular6) - How to pass async data to child components in an object (Angular6) 以角度访问其他组件数组中的子组件数据 - accessing child components data in other component's array in angular 如果它们不是Angular2中的父子组件,如何在组件之间传递数据? - How to pass data between components, if they are not parent-child components in Angular2? Angular - 如何将路由功能传递给子组件 - Angular - How to pass routing function to child components 组件之间的Angular 2 Pass数据 - Angular 2 Pass data between components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM