对不起,如果标题混乱,不知道该怎么说。我试图调用函数来获取数据并将其传递给子组件。
父.component.ts
以下内容:
getData(arg: string[]) {
let data: any[];
// Do stuff here to get data
return data;
}
父.component.html
以下内容:
<app-child [myData]="getData(['test'])"></app-child>
子组件.ts
export class ChildComponent {
@Input() myData: any[];
// Stuff here
}
child.component.html子组件
<h3>{{ myData[0].test }}</h3>
但是,当我运行应用程序时,我会得到一个错误
Cannot read property '0' of undefined
是的。我做错什么了?我不想创建要绑定到的变量
[myData]
因为那样我就有很多了。我也会在其他地方使用这个功能。
任何帮助都将不胜感激,谢谢!