[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(); }, );
- }
- ngOnInit() {
- }
- }
There is another API: reattach() which uses for reset to default Angular change dectctor.
[Angular] Angular Custom Change Detection with ChangeDetectorRef的更多相关文章
- angular 2 - 006 change detection 脏治检查 - DC
ANGULAR CHANGE DETECTION EXPLAINED 引发脏治检查有三种方式: Events - click, submit, - XHR - Fetching data from a ...
- CHANGE DETECTION IN ANGULAR 2
In this article I will talk in depth about the Angular 2 change detection system. HIGH-LEVEL OVERVIE ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- [Audio processing] Harmonic change detection function (HCDF)
Harmonic change detection function (HCDF) 是根据 Tonal Centroid (TC)实现的,首先TC如何提取? Step 1. 提取PCP特征 Step ...
- angular 有关侦测组件变化的 ChangeDetectorRef 对象
我们知道,如果我们绑定了组件数据到视图,例如使用 <p>{{content}}</p>,如果我们在组件中改变了content的值,那么视图也会更新为对应的值. angular ...
- [Angular] Http Custom Headers and RequestOptions
updatePassenger(passenger: Passenger): Observable<Passenger> { let headers = new Headers({ 'Co ...
- [Angular] Create custom validators for formControl and formGroup
Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...
- [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 ...
- [Angular 2] Custom Validtors
Create a custom validtor which only accepts the string start with '123'; function skuValidator(contr ...
随机推荐
- Mac-WIFI总是断网
$ cd /Library/Preferences/SystemConfiguration/ 删除如下文件 com.apple.airport.preferences.plist com.apple. ...
- Spring容器整合WebSocket
原链接:http://blog.csdn.net/canot/article/details/52575054 WebSocker是一个保持web客户端与服务器长链接的技术.这样在两者通信过程中如果服 ...
- JS ajxa请求 返回数据
1. 发送ajax请求, 后台返回json集合 JQuery: $.each(list集合,回调函数function(下标,集合对象){}); 如下: <script> $(func ...
- Tkinter 小应用
import tkinter as tk class APP: def __init__(self,master): frame = tk.Frame(master) frame.pack(side ...
- hdu 2196(方法1:经典树形DP+方法2:树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)
本篇文章是使用python抓取数据的第一篇,使用requests+BeautifulSoup的方法对页面进行抓取和数据提取.通过使用requests库对链家网二手房列表页进行抓取,通过Beautifu ...
- Codeforces Round 254 (Div. 2)
layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- next_permutatio
next_permutation的函数声明:#include <algorithm> bool next_permutation( iterator start, iterator en ...
- 带WHERE子句的UPDATE语句
目前演示的几个UPDATE语句都是一次性更新所有行的数据,这无法满足只更新符合特定条件的行的需求,比如“将Tom 的年龄修改为12 岁”.要实现这样的功能只要使用WHERE 子句就可以了,在WHERE ...
- small test on 5.30 night T1
数学题使劲推就对了. 让我们设 g(x) = ∑ C(i,x) * b^i ,然后后面验算了一张纸QWQ,懒得再打一遍了,回家我就把这张演算纸补上QWQ,先上代码. #include<cstd ...