[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 ...
随机推荐
- 取消SecureCRT的右击粘贴功能
默认为选中时自动复制,右键粘贴 要取消的话在: Options->Global Options ...->Terminal 里面有个Mouse的选项块. Paste on Right/Le ...
- Delphi字符串、PChar与字符数组之间的转换
来自:http://my.oschina.net/kavensu/blog/193719 ------------------------------------------------------- ...
- k8s的Rolling Update(滚动更新应用)
滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性. 下面我们部署三副本应用: 初始 ...
- Redis-4.0.8 readme.md
This README is just a fast *quick start* document. You can find more detailed documentation at [redi ...
- hdu 1227(动态规划)
Fast Food Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- kotlin 写的一个简单 sql 查询解析器
package com.dx.efuwu.core import org.apache.commons.lang.StringUtils import java.sql.PreparedStateme ...
- HSV做通道分离是出现的Vector内存越界错误
vector<Mat> hsvSplit; //因为我们读取的是彩色图,直方图均衡化需要在HSV空间做 split(imgHSV, hsvSplit); equalizeHist(hsvS ...
- HDU 1017 A Mathematical Curiosity (输出格式,穷举)
#include<stdio.h> int main() { int N; int n,m; int a,b; int cas; scanf("%d",&N); ...
- 洛谷——P1163 银行贷款
P1163 银行贷款 题目描述 当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款.这个问题要求计算出贷款者向银行支付的利率.假设利率按月累计. 输入输出格式 输入格式: 输入文 ...