zoukankan      html  css  js  c++  java
  • [Angular] Angular Custom Change Detection with ChangeDetectorRef

    Each component has its own ChangeDetectorRef, and we can inject ChangeDetectorRef into constructor:

    export class ChildComponent implements OnInit {
    
      constructor(
        private cdr: ChangeDetectorRef
      )

    For example if you have a huge list can be updated in real time though some real time database.

    Update in real time is really expensive for huge list. 

    We have build a custom change detector, for example, update every 5 seconds, to check changes, to do that, we need to two things,

    1. Disable default change detector

    2. Check for changes every 5 seconds.

    import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
    import { ListService } from './list.service';
    
    @Component({
      selector: 'app-child',
      templateUrl: './child.component.html',
      styleUrls: ['./child.component.css']
    })
    export class ChildComponent implements OnInit {
    
      constructor(
        private cdr: ChangeDetectorRef,
        private dataProvider: ListService
      ) { 
        // disable default change detector
        cdr.detach();
        // now every 5second, do a check for its child tree
        setInterval(() => { this.cdr.detectChanges(); }, 5000);
      }
    
      ngOnInit() {
      }
    
    }

    There is another API: reattach() which uses for reset to default Angular change dectctor.

  • 相关阅读:
    hdu2063 匈牙利算法 二分最大匹配模版题
    经典dp 编辑距离
    新博客的第一道题 蓝桥杯 蚂蚁感冒
    cv.GaussianBlur参数选择 && contrast stretching
    大数据 week2 Hadoop and HDFS
    大数据 week2 Hadoop and HDFS
    抖音二面记录
    weight decay
    Pillow Image Filter
    UNSW CV第三课 下
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10331474.html
Copyright © 2011-2022 走看看