DoCheck
一个生命周期钩子,除了使用默认的变更检查器执行检查之外,还会为指令执行自定义的变更检测函数。
A lifecycle hook that invokes a custom change-detection function for a directive, in addition to the check performed by the default change-detector.
interface DoCheck {
ngDoCheck(): void
}
参见
说明
在变更检测期间,默认的变更检测算法会根据引用来比较可绑定属性,以查找差异。 你可以使用此钩子来用其他方式检查和响应变更。
The default change-detection algorithm looks for differences by comparing bound-property values by reference across change detection runs. You can use this hook to check for and respond to changes by some other means.
当默认的变更检测器检查更改时,它会执行 ngOnChanges()
(如果有),而不在乎你是否进行了额外的变更检测。 一般来说,你不应该同时使用 DoCheck
和 OnChanges
这两个钩子来响应在同一个输入上发生的更改。
When the default change detector detects changes, it invokes ngOnChanges()
if supplied, regardless of whether you perform additional change detection. Typically, you should not use both DoCheck
and OnChanges
to respond to changes on the same input.
Further information available in the Usage Notes...
方法
一个回调方法。它会在默认的变更检测器执行之后调用,并进行变更检测。 参见 A callback method that performs change-detection, invoked after the default change-detector runs. See |
参数没有参数。 返回值
|
使用说明
下列代码片段展示了组件如何实现该接口,以执行自定义的变更检测周期。
The following snippet shows how a component can implement this interface to invoke it own change-detection cycle.
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements DoCheck {
ngDoCheck() {
// ...
}
}
For a more complete example and discussion, see Defining custom change detection.