加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[Angular] Angular Custom Change Detection with ChangeDetecto

发布时间:2020-12-17 07:02:45 所属栏目:安全 来源:网络整理
导读: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 u

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(); },5000);
  }

  ngOnInit() {
  }

}

?

There is another API: reattach() which uses for reset to default Angular change dectctor.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读