Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable angular2 change detection for 3rd party libraries

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

Click here for map preview

it will be even worse when using mouseover event.

ngDoCheck() {
  console.log('do check', this.i++);
}
like image 202
tarmo Avatar asked Aug 17 '16 11:08

tarmo


People also ask

How do you stop change detection?

Click on "Enable Change Detection" from the given list. Select "Enable" or "Disable" operation based on your requirement. If you select Disable, skip to the last step.

What triggers Angular change detection?

Angular runs its change detection mechanism periodically so that changes to the data model are reflected in an application's view. Change detection can be triggered either manually or through an asynchronous event (for example, a user interaction or an XMLHttpRequest completion).

What triggers OnPush change detection?

But with OnPush strategy, the change detector is only triggered if the data passed on @Input() has a new reference. This is why using immutable objects is preferred, because immutable objects can be modified only by creating a new object reference.

What is Changedetectionstrategy OnPush in Angular?

The main idea behind the OnPush strategy manifests from the realization that if we treat reference types as immutable objects, we can detect if a value has changed much faster. When a reference type is immutable, this means every time it is updated, the reference on the stack memory will have to change.


1 Answers

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

constructor(private zone: NgZone) {

)

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.

Thanks to this

like image 109
Sergio Contreras Sustaita Avatar answered Nov 02 '22 11:11

Sergio Contreras Sustaita