OnChanges
一个生命周期钩子,当指令的任何一个可绑定属性发生变化时调用。 定义一个 ngOnChanges()
方法来处理这些变更。
A lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges()
method to handle the changes.
interface OnChanges {
ngOnChanges(changes: SimpleChanges): void
}
参见
方法
如果至少发生了一次变更,则该回调方法会在默认的变更检测器检查完可绑定属性之后、视图子节点和内容子节点检查完之前调用。 A callback method that is invoked immediately after the default change detector has checked data-bound properties if at least one has changed, and before the view and content children are checked. | |||
参数
返回值
|
使用说明
下列代码片段展示了组件要如何实现本接口来定义一个输入属性的变更处理器。
The following snippet shows how a component can implement this interface to define an on-changes handler for an input property.
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnChanges {
// TODO(issue/24571): remove '!'.
@Input() prop!: number;
ngOnChanges(changes: SimpleChanges) {
// changes.prop contains the old and the new value...
}
}