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. idea配置maven自动下载 源码和文档

    勾上图中红框处,即可

  2. C# for Hbase 实现详解

    一共两种方式访问 通过Thrift访问 目前hbase src.tar.gz压缩包中包含thrift he thrift2; 根据官方文档,thrift可能被抛弃,但是网上基本上都是介绍thrift的 ...

  3. 扩展 RequestHandlerBase

    RequestHandlerBase 实现接口SolrRequestHandler SearchHandler: 它的所有逻辑来自 搜索组件SearchComponents.  handler配置中, ...

  4. hdu 1932(spfa)

    XYZZY Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3694   Accepted: 1059 Description ...

  5. Linux删除重复行

    本文转自http://blog.csdn.net/ithomer/article/details/6926325 文本处理时,经常要删除重复行,下面是三种方法 第一,用sort+uniq,注意,单纯u ...

  6. STL心得

    熟悉c++版算法竞赛程序框架 理解变量引用的原理 熟练掌握string和stringstream 熟练掌握c++结构体的定义和使用,包括构造函数和静态成员变量 了解常见的可重载运算符,包括四则运算,赋 ...

  7. Apache Kafka 企业级消息队列

    1.大纲 了解 Apache Kafka是什么 掌握Apache Kafka的基本架构 搭建Kafka集群 掌握操作集群的两种方式 了解Apache Kafka高级部分的内容 2.消息系统的作用是什么 ...

  8. 卡特兰数 3134 Circle

    3134 Circle  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 在一个圆上,有2*K个不同的结点,我们 ...

  9. 51nod 子序列的个数(动态规划)

    子序列的个数 给定一个正整数序列,序列中元素的个数和元素值大小都不超过105, 求其所有子序列的个数.注意相同的只算一次:例如 {1,2,1}有子序列{1} {2} {1,2} {2,1}和{1,2, ...

  10. BZOJ 1878 [SDOI2009]HH的项链(扫描线+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1878 [题目大意] 给出一个数列,给出m个查询,每次查询一个区间中不相同的数字个数 [ ...