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(); }, );
} ngOnInit() {
} }

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

[Angular] Angular Custom Change Detection with ChangeDetectorRef的更多相关文章

  1. angular 2 - 006 change detection 脏治检查 - DC

    ANGULAR CHANGE DETECTION EXPLAINED 引发脏治检查有三种方式: Events - click, submit, - XHR - Fetching data from a ...

  2. CHANGE DETECTION IN ANGULAR 2

    In this article I will talk in depth about the Angular 2 change detection system. HIGH-LEVEL OVERVIE ...

  3. [Angular & Unit Testing] Automatic change detection

    When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...

  4. [Audio processing] Harmonic change detection function (HCDF)

    Harmonic change detection function (HCDF) 是根据 Tonal Centroid (TC)实现的,首先TC如何提取? Step 1. 提取PCP特征 Step ...

  5. angular 有关侦测组件变化的 ChangeDetectorRef 对象

    我们知道,如果我们绑定了组件数据到视图,例如使用 <p>{{content}}</p>,如果我们在组件中改变了content的值,那么视图也会更新为对应的值. angular ...

  6. [Angular] Http Custom Headers and RequestOptions

    updatePassenger(passenger: Passenger): Observable<Passenger> { let headers = new Headers({ 'Co ...

  7. [Angular] Create custom validators for formControl and formGroup

    Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...

  8. [Angular] Read Custom HTTP Headers Sent by the Server in Angular

    By default the response body doesn’t contain all the data that might be needed in your app. Your ser ...

  9. [Angular 2] Custom Validtors

    Create a custom validtor which only accepts the string start with '123'; function skuValidator(contr ...

随机推荐

  1. phpstudy2014没有mysqldumpslow.pl及其在性能优化的作用

    mysqldumpslow.pl的作用是监控mysql的性能瓶颈的 1)在phpstudy2014中没有这个mysqldumpslow.pl所以需要去下载一个然后放置于mysql/bin的文件夹中,由 ...

  2. selenium 警告框处理 (弹窗处理)

    在web应用中常常会遇见很多用JavaScript编写的alert .confirm 以及prompt 弹窗,这是就需要driver.switchTo().alert()来选取(定位)警告弹窗.再对弹 ...

  3. k8s job的使用

    1.运行一次性容器 容器按照持续运行的时间可分为两类: 服务类容器 服务类容器通常持续提供服务,需要一直运行,比如 http server,daemon 等. 工作类容器 工作类容器则是一次性任务,比 ...

  4. hdu 5194(DFS)

    DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. hdu 5178(二分-lower_bound,upper_bound)

    pairs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. centos 命令行修改主机名

    # vi /etc/sysconfig/network # 把localhost.localdomain 修改为 localhost.com # 保存退出 # vi /etc/hosts # 把loc ...

  7. UTC时间

    世界的每个地区都有自己的本地时间,在Internet及无线电通信时,时间的统一非常重要! 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用 ...

  8. 推荐下载CentOS下各种组件的网址

    http://vault.centos.org/ ftp://mirrors.sohu.com/ http://mirror.webtatic.com/

  9. CentOS 7 下nagios搭建记录

    跟随 园子的文章搭建 http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html 1.遇 nagios插件地址迁移错误,记录解决. 2. ...

  10. Codeforces 920 F SUM and REPLACE

    Dicription Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) =  ...