简体   繁体   English

无法将数据向下传递到ngFor循环中的角度子组件?

[英]Can't pass data down to angular child component in ngFor loop?

I'm trying to pass the data "testData" down to the child component 'multi-card' but when I attempt to do so it returns this error. 我正在尝试将数据“ testData”向下传递给子组件“多卡”,但是当我尝试这样做时,它将返回此错误。

Uncaught Error: Template parse errors: Can't bind to 'data' since it isn't a known property of 'app-multi-card' 未捕获的错误:模板解析错误:无法绑定到“数据”,因为它不是“应用程序多卡”的已知属性

I'm new to angular so I'm pretty sure I'm missing something obvious. 我是新手,所以我很确定自己缺少明显的东西。

Parent TS 家长TS

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-information-general-multi',
  templateUrl: './information-general-multi.component.html',
  styleUrls: ['./information-general-multi.component.scss']
})
export class InformationGeneralMultiComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  testData = [
    {
      "name": "bingo"
    },
    {
      "name": "notta"
    }
  ]

}

Parent Template 父模板

    <div class='mdl-grid mdl-grid--no-spacing'>

  <div class='mdl-cell mdl-cell--6-col mdl-cell--8-col-tablet' *ngFor="let test of testData">
        <app-multi-card [data]="testData"></app-multi-card>
      </div>
    </div>

I haven't tried to call it in my child component am I supposed to import the data in child component or does that happen automatically? 我没有尝试在子组件中调用它,应该将数据导入子组件中还是会自动发生?

You have to use the decorator @Input() on a property called data in your child component. 您必须在子组件中名为data的属性上使用装饰器@Input() This property will be fed automatically by the binding. 绑定将自动提供此属性。

Have a look in the documentation for this : https://angular.io/guide/component-interaction 看看文档: https : //angular.io/guide/component-interaction

Use [data]="test" as follows: 使用[data]="test" ,如下所示:

<div class='mdl-cell mdl-cell--6-col mdl-cell--8-col-tablet' *ngFor="let test of testData">
        <app-multi-card [data]="testData"></app-multi-card>
      </div>

Also, in app-multi-card component ts file, have you given @Input() declaration for data property? 另外,在app-multi-card组件ts文件中,您是否为data属性指定了@Input()声明?

@Input() data = ''

Refer https://angular.io/guide/component-interaction 请参阅https://angular.io/guide/component-interaction

test代替testData放入以下sendense中,并在app-multi-card组件中添加@Input data装饰器

<app-multi-card [data]="test"></app-multi-card>

By default all the properties of the component are accessible within the component, to expose the property in a parent component to child component you need to explicitly mention by adding a decorator @Input() infront of the property. 默认情况下,组件的所有属性都可以在组件内访问,要将父组件中的属性公开给子组件,您需要通过在属性前面添加装饰器@Input()来明确提及。
In your child-component.ts 在您的child-component.ts中

import Input frm @angular/core.
@Input() data;

then you can access test which you are assigning in the parent-component. 那么您就可以访问要在父组件中分配的测试

<div class='mdl-cell mdl-cell--6-col mdl-cell--8-col-tablet' *ngFor="let test of testData">
    <app-multi-card [data]="test"></app-multi-card>
</div>

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

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