在我的角度項目,讓說,我有如下設置:角傳球變量
主頁:
的.html:
<div class="main component" *ngFor="let item of items;">
<my-card [info]="item"></my-card>
</div>
組件頁:
。:
import { Component, Input } from '@angular/core';
@Component({
selector: 'my-card',
templateUrl: 'myCard.html'
})
export class myCard{
@Input() info: Array<any>;
}
的.html:
<div class="subComponent"> {{info}}</div>
的想法是使用主網頁,其工作只是正常的內部myCard
HTML模板。
但不知何故info
是.ts
文件內部未定義的。
我該如何更改代碼,以便定義main page
的info
變量?
export class myCard{
@Input() info: Array<any>;
console.log(info); //Currently undefined
};
item不是數組,我認爲;所以這應該是@Input()info:any; –
您是否嘗試過使用'ngOnInit'鉤子? – yurzui
要明確,'console.log'語句應該放在'ngOnInit'裏面 – Skeptor