你好我有一個小問題,從一個父組件傳遞數據到一個子組件控制器。將Angular2中的數據傳遞給子組件
這就是我的父Component parent.component.html
<element [mydata]="Value"></element>
的標記代碼有了這個,我設置MYDATA與價值。
我設置我的孩子控制器
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'element',
templateUrl: './element.component.html',
styleUrls: ['./element.component.scss'],
})
export class ElementComponent implements OnInit {
@Input() public mydata:string;
ngOnInit() {
console.log(this.mydata);
}
}
使用@input這個值,我得到一個未定義的控制檯,但是爲什麼呢?
'價值'從Ajax填充? –
不,這適用於我''元素[mydata] =「值」> {{價值}}'但我怎麼能在構造函數中使用這個變量? – greenchapter
爲什麼你想要在構造函數中獲得'value' ..你應該使用生命週期事件... –