为什么组件上的输入属性在构造函数
中不可用@Input() results: Result[];
constructor() {
console.log(this.results); // why it is not available here
}
答案 0 :(得分:6)
输入属性未初始化,因此通常您可以访问 ngOnInit()
上的输入值检查LifeCycle。 https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html
import {Component, Input} from 'angular2/angular2'
@Component({
selector: 'child',
template: `
<p>The next number is {{ mynumber + 1 }}</p>
`
})
class ChildComponent {
@Input() mynumber: number;
ngOnInit(){
console.log(this.number);
}
}
@Component({
selector: 'parent',
template: `
<child [mynumber]="41"></child>
`
})
export class ParentComponent {}