I'm using a Jquery plugin in my angular2 project.
It has a serious performance problem if the plugin has an event like $(window).mousemove() or setInterval(function(){}, 10). Because zone.js has hooks to html events, the page always checks data changes. My cpu is running very high.
-
Read about OnPushEran Shabi– Eran Shabi2016-12-06 07:24:58 +00:00Commented Dec 6, 2016 at 7:24
-
Possible duplicate of Prevent a native browser event ( like scroll ) from firing change detectionMilad– Milad2016-12-06 09:02:12 +00:00Commented Dec 6, 2016 at 9:02
Add a comment
|
2 Answers
You can use NgZone to make code run inside or outside of Angulars zone.
Outside for performance reasons, inside to ensure Angular gets notified that change detection has to be run when properties of a component, model, or service are modiefied:
constructor(private zone:NgZone) {
zone.runOutsideAngular(() => {
$(window).mousemove()...;
setInterval(() => {}, 10);
})
}
onMouseMove() {
this.zone.run(() => {
this.someProp = 'someValue';
this.router.navigate(....)
});
}
9 Comments
Moon
ngZone.runOutsideAngular is working doCheck call once. but if the route jumps doCheck always call.
Günter Zöchbauer
What do you mean by "if the route jumps doCheck"?
Günter Zöchbauer
I don't get how routing is related to your question.
Moon
from router '/a' to router '/b' runOutsideAngular is not work. refresh '/b' runOutsideAngular is work
Günter Zöchbauer
That's why I added ` this.router.navigate(....)` as example inside
this.zone.run(). router.navigate() depends on change detection to happen. With runOutsideAngular() updating the view won't work. |