我试图理解angular2中的OnInit功能并阅读文档:
Description
Implement this interface to execute custom initialization logic after
your directive’s data-bound properties have been initialized.ngOnInit is called right after the directive’s data-bound properties
have been checked for the first time,and before any of its children
have been checked. It is invoked only once when the directive is
instantiated.
我不明白指令的数据绑定属性是什么意思?
当你有一个组件
@Component({ selector: 'my-component' }) class MyComponent { @Input() name:string; ngOnChanges(changes) { } ngOnInit() { } }
你可以像使用它一样
<my-component [name]="somePropInParent"></my-component>
更改somePropInParent的值时,Angulars更改检测更新名称并调用ngOnChanges()
在第一次调用ngOnChanges()之后,调用ngOnInit()一次,以指示解析并应用了初始绑定([name] =“somePropInParent”).
有关详细信息,请参阅https://angular.io/docs/ts/latest/cookbook/component-communication.html