如何为 3rd 方库禁用 angular2 更改检测

how to disable angular2 change detection for 3rd party libraries

我有 google 个地图,每秒触发 100 多次变化检测。如何为此禁用更改检测。

Click here for map preview

使用mouseover事件会更糟

ngDoCheck() {
  console.log('do check', this.i++);
}

我遇到了同样的问题,请尝试在组件构造函数中注入 NgZone class

constructor(private zone: NgZone) {

)

然后,使用 NgZone 中的 runOutsideAngular 方法将 google 图表中的 draw 方法放入回调中,执行类似这样的操作。

this.zone.runOutsideAngular(() => {
    var chart = new google.visualization.PieChart(nativeElement);
    chart.draw(dataTable, options);
})

这使得执行的代码不会触发 angular 检测更改。 将此应用于您制作的每个图表。希望对您有所帮助。

Thanks to this

临时禁用更改检测的另一个选项ChangeDetectorRef

enabled = true;  
constructor(private ref: ChangeDetectorRef)

toggleChangeDetection() {
  if (this.enabled) 
  {
    this.enabled = false;
    this.ref.detach();
  }
  else {
    this.enabled = true;
    this.ref.reattach();
}