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

  1. export class ChildComponent implements OnInit {
  2.  
  3. constructor(
  4. private cdr: ChangeDetectorRef
  5. )

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.

  1. import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
  2. import { ListService } from './list.service';
  3.  
  4. @Component({
  5. selector: 'app-child',
  6. templateUrl: './child.component.html',
  7. styleUrls: ['./child.component.css']
  8. })
  9. export class ChildComponent implements OnInit {
  10.  
  11. constructor(
  12. private cdr: ChangeDetectorRef,
  13. private dataProvider: ListService
  14. ) {
  15. // disable default change detector
  16. cdr.detach();
  17. // now every 5second, do a check for its child tree
  18. setInterval(() => { this.cdr.detectChanges(); }, );
  19. }
  20.  
  21. ngOnInit() {
  22. }
  23.  
  24. }

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. Mac-WIFI总是断网

    $ cd /Library/Preferences/SystemConfiguration/ 删除如下文件 com.apple.airport.preferences.plist com.apple. ...

  2. Spring容器整合WebSocket

    原链接:http://blog.csdn.net/canot/article/details/52575054 WebSocker是一个保持web客户端与服务器长链接的技术.这样在两者通信过程中如果服 ...

  3. JS ajxa请求 返回数据

    1. 发送ajax请求, 后台返回json集合    JQuery: $.each(list集合,回调函数function(下标,集合对象){}); 如下: <script> $(func ...

  4. Tkinter 小应用

    import tkinter as tk class APP: def __init__(self,master): frame = tk.Frame(master) frame.pack(side ...

  5. hdu 2196(方法1:经典树形DP+方法2:树的直径)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)

    本篇文章是使用python抓取数据的第一篇,使用requests+BeautifulSoup的方法对页面进行抓取和数据提取.通过使用requests库对链家网二手房列表页进行抓取,通过Beautifu ...

  7. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  8. next_permutatio

    next_permutation的函数声明:#include  <algorithm> bool next_permutation( iterator start, iterator en ...

  9. 带WHERE子句的UPDATE语句

    目前演示的几个UPDATE语句都是一次性更新所有行的数据,这无法满足只更新符合特定条件的行的需求,比如“将Tom 的年龄修改为12 岁”.要实现这样的功能只要使用WHERE 子句就可以了,在WHERE ...

  10. small test on 5.30 night T1

    数学题使劲推就对了. 让我们设  g(x) = ∑ C(i,x) * b^i ,然后后面验算了一张纸QWQ,懒得再打一遍了,回家我就把这张演算纸补上QWQ,先上代码. #include<cstd ...