我试图理解angular2中的OnInit功能并阅读文档:
描述
在初始化指令的数据绑定属性后,实现此接口以执行自定义初始化逻辑.
在第一次检查指令的数据绑定属性之后,以及在检查其任何子项之前,立即调用ngOnInit.实例化指令时仅调用一次.
我不明白directive's data-bound properties这是什么意思?
Gün*_*uer 25
当你有一个组件
@Component({
selector: 'my-component'
})
class MyComponent {
@Input() name:string;
ngOnChanges(changes) {
}
ngOnInit() {
}
}
Run Code Online (Sandbox Code Playgroud)
你可以像使用它一样
<my-component [name]="somePropInParent"></my-component>
Run Code Online (Sandbox Code Playgroud)
这使得name数据绑定属性.
当值somePropInParent改变时,Angulars会更改检测更新name和调用ngOnChanges()
在ngOnChanges()第一次ngOnInit()调用之后,调用一次,以指示初始绑定([name]="somePropInParent")已解决并应用.
有关详细信息,请参阅https://angular.io/docs/ts/latest/cookbook/component-communication.html