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. js判断对象为空

    http://www.jb51.net/article/42713.htm var isEmptyValue = function(value) { var type; if(value == nul ...

  2. Chief Technology Officer

    转自地址:http://www.swoole.com/News/76.html 1)错误都是自上而下 当事情出现混乱的时候,人们总是寻求寄托于Process的制定,很多的管理者,觉察到事情的失控,却不 ...

  3. UVA 12594 Naming Babies

    $dp$,斜率优化. 设$dp[j][i]$表示前$i$个位置分成$j$段的最小值,递推式很好写,预处理几个前缀和就可以了,然后斜率优化即可. #pragma comment(linker, &quo ...

  4. 大数据技术之_16_Scala学习_07_数据结构(上)-集合

    第十章 数据结构(上)-集合10.1 数据结构特点10.1.1 Scala 集合基本介绍10.1.2 可变集合和不可变集合举例10.2 Scala 不可变集合继承层次一览图10.2.1 图10.2.2 ...

  5. USACO1.3.2修理牛棚

    在学习一段时间贪心并写了一些贪心题之后,又一次看到了农夫和牛幸福美满的生活故事(雾).嘛,闲话少说,上题目 在一个暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满 ...

  6. Jenkins获取分支的插件

    Jenkins--->xxxx--->配置--->参数化构建过程--->选择Git Parameter Plug-In插件 Name: git_branch Descripti ...

  7. RPD Volume 168 Issue 4 March 2016 评论5

    Monte Carlo simulation of secondary radiation exposure from high-energy photon therapy using an anth ...

  8. Miller-Rabin与Pollard-Rho备忘

    Miller-Rabin素性测试算法: 根据费马小定理当p为素数时成立,所以如果存在一个a使x不满足此定理,则x必然不为素数. 但这是充分条件而不是必要条件,所以对于每个a,可能存在满足定理的x,这时 ...

  9. 【kd-tree】bzoj3290 Theresa与数据结构

    离线所有操作,对所有可能存在的点建立kd-tree,add相当于权值+1,cancel相当于权值-1. 修改操作要记录kd-tree上每个点的fa,从底向上地进行修改. 优化:若一个矩形框的sumv= ...

  10. 【网络流】【Dinic】【Next Array】Dinic模板

    注意:有时加边不一定要加反向弧. Next Array版. #include<cstdio> #include<cstring> #include<algorithm&g ...