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()(如果有),而不在乎你是否进行了额外的变更检测。 一般来说,你不应该同时使用 DoCheckOnChanges 这两个钩子来响应在同一个输入上发生的更改。

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.

方法

一个回调方法。它会在默认的变更检测器执行之后调用,并进行变更检测。 参见 KeyValueDiffersIterableDiffers,以实现针对集合对象的自定义变更检测逻辑。

A callback method that performs change-detection, invoked after the default change-detector runs. See KeyValueDiffers and IterableDiffers for implementing custom change checking for collections.

ngDoCheck(): void
      
      ngDoCheck(): void
    
参数

没有参数。

返回值

void

使用说明

下列代码片段展示了组件如何实现该接口,以执行自定义的变更检测周期。

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() { // ... } }
      
      @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements DoCheck {
  ngDoCheck() {
    // ...
  }
}