如何禁用第三方库的angular2更改检测 [英] how to disable angular2 change detection for 3rd party libraries
问题描述
我有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屋!