如何禁用第三方库的angular2更改检测 [英] how to disable angular2 change detection for 3rd party libraries

查看:82
本文介绍了如何禁用第三方库的angular2更改检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Google地图,每秒可触发100多次更改检测.如何为此禁用更改检测.

I have google maps which triggers 100+ times per second change detection. how to disable change detection for this.

点击此处查看地图预览

使用鼠标悬停事件时,情况甚至更糟.

it will be even worse when using mouseover event.

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

推荐答案

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

I had the same issue, try injecting the NgZone class on your component constructor

constructor(private zone: NgZone) {

)

然后,使用NgZone中的runOutsideAngular方法将回调从Google图表中放入draw方法中,

then, use the runOutsideAngular method from NgZone to put in a callback the draw method from google charts, do something like this.

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

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

This make the executed code don't fire angular detection changes. Apply this for each chart you make. I hope find this helpful.

感谢

这篇关于如何禁用第三方库的angular2更改检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆