我试图了解 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.
我不明白指令的数据绑定(bind)属性
是什么意思?
最佳答案
当你有一个组件时
@Component({
selector: 'my-component'
})
class MyComponent {
@Input() name:string;
ngOnChanges(changes) {
}
ngOnInit() {
}
}
你可以这样使用它
<my-component [name]="somePropInParent"></my-component>
这使 name
成为数据绑定(bind)属性。
当 somePropInParent
的值发生变化时,Angulars 变化检测会更新 name
并调用 ngOnChanges()
在第一次调用 ngOnChanges()
之后,调用一次 ngOnInit()
,表示初始绑定(bind) ([name]="somePropInParent"
) 已解决并应用。
有关详细信息,请参阅 https://angular.io/docs/ts/latest/cookbook/component-communication.html
关于angular - 什么是数据绑定(bind)属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367423/