Link to the artical.

Zone detects any async opreations. Once an async oprations happens in Angular, Zone will notify change detection to kick in.

Images we have 5000 svg box displaying on the screen.

And each svg elements and three event listener on it:

@Component({
...
template: `
<svg (mousedown)="mouseDown($event)"
(mouseup)="mouseUp($event)"
(mousemove)="mouseMove($event)"> <svg:g box *ngFor="let box of boxes" [box]="box">
</svg:g> </svg>
`
})
class AppComponent {
...
}

Three (3) event handlers are bound to the outer SVG element. When any of these events fire and their handlers have been executed then change detection is performed. In fact, this means that Angular will run change detection, even when we just move the mouse over the boxes without actually dragging a single box!

Since 'mousemove' is the event cause the change detection which is not necessary. So we simple remove it from the HTML:

<svg (mousedown)="mouseDown($event)"
(mouseup)="mouseUp($event)"> <svg:g box *ngFor="let box of boxes" [box]="box">
</svg:g> </svg>

So now we need a way to attach 'mousemove' event for only the selected svg box. We can do this inside 'mousedown' event:

  constructor(private zone: NgZone) {}

  mouseDown(event) {
...
this.element = event.target; this.zone.runOutsideAngular(() => {
window.document.addEventListener('mousemove', this.mouseMove.bind(this));
});
}

We inject NgZone and call runOutsideAngular() inside our mouseDown() event handler, in which we attach an event handler for the mousemove event. This ensures that the mousemove event handler is really only attached to the document when a box is being selected.

  mouseMove(event) {
event.preventDefault();
this.element.setAttribute('x', event.clientX + this.clientX + 'px');
this.element.setAttribute('y', event.clientX + this.clientY + 'px');
}

In addition, we save a reference to the underlying DOM element of the clicked box so we can update its x and y attributes in the mouseMove() method. We’re working with the DOM element instead of a box object with bindings for x and y, because bindings won’t be change detected since we’re running the code outside Angular’s Zone. In other words, we do update the DOM, so we can see the box is moving, but we aren’t actually updating the box model (yet).

In the next step, we want to make sure that, whenever we release a box (mouseUp), we update the box model, plus, we want to perform change detection so that the model is in sync with the view again. The cool thing about NgZone is not only that it allows us to run code outside Angular’s Zone, it also comes with APIs to run code inside the Angular Zone, which ultimately will cause Angular to perform change detection again. All we have to do is to call NgZone.run() and give it the code that should be executed.

Here’s the our updated mouseUp() event handler:

@Component(...)
export class AppComponent {
...
mouseUp(event) {
// Run this code inside Angular's Zone and perform change detection
this.zone.run(() => {
this.updateBox(this.currentId, event.clientX + this.offsetX, event.clientY + this.offsetY);
this.currentId = null;
}); window.document.removeEventListener('mousemove', this.mouseMove);
}
}

Also notice that we’re removing the event listener for the mousemove event on every mouseUp. Otherwise, the event handler would still be executed on every mouse move. In other words, the box would keep moving even after the finger was lifted, essentially taking the drop part out of drag and drop. In addition to that, we would pile up event handlers, which could not only cause weird side effects but also blows up our runtime memory.

Notice:

However, we shouldn’t forget that this solution also comes with a couple (probably fixable) downsides. For example, we’re relying on DOM APIs and the global window object, which is something we should always try to avoid. If we wanted to use this code with on the server-side then direct access of the window variable would be problematic. We will discus these server-side specific issues in a future article. For the sake of this demo, this isn’t a big deal though.

[Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE的更多相关文章

  1. Angular学习笔记:Angular CLI

    定义 Angular CLI:The Angular CLI is a command line interface tool that can create a project, add files ...

  2. angular enter事件,angular回车事件

    angular回车键搜索,angular enter搜索 对于搜索框,用户在输入内容后的搜索习惯不是鼠标点击搜索按钮,而是直接按enter键,那我们怎么给enter键绑定事件呢,其实很简单,代码如下: ...

  3. Angular 2 升级到 Angular 5

    Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...

  4. Angular系列一:Angular程序架构

    Angular程序架构 Angular程序架构 组件:一段带有业务逻辑和数据的Html服务:用来封装可重用的业务逻辑指令:允许你向Html元素添加自定义行为模块: 环境搭建 安装nodeJs安装好no ...

  5. Angular企业级开发(3)-Angular MVC实现

    1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...

  6. 30行代码让你理解angular依赖注入:angular 依赖注入原理

    依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...

  7. angular源码分析:angular中脏活累活承担者之$parse

    我们在上一期中讲 $rootscope时,看到$rootscope是依赖$prase,其实不止是$rootscope,翻看angular的源码随便翻翻就可以发现很多地方是依赖于$parse的.而$pa ...

  8. 精通 Angular JS 第一天——Angular 之禅

    简介 Angular JS是采用JavaScript语言编写的客户端MVC框架,它为业界带了重大的变化,包括对模板化的创新实现,以及数据的双向绑定,这些特性使得它强大而易用.它可以用来帮助开发者编写单 ...

  9. [Angular 2] Create Shareable Angular 2 Components

    Components that you use across multiple applications need to follow a module pattern that keeps them ...

随机推荐

  1. 记2018/5/5 qbxt 测试

    记2018/5/5 qbxt 测试 竞赛时间: 2018 年 5 月 5 日 13:30-17:00 T1 一.maze(1s,512MB): 简单的迷宫问题:给定一个n*m的迷宫,.表示可以通过,# ...

  2. iOS界面生命周期过程具体解释

    开发过Android的人都知道,每个Android界面就是一个Activity,而每个Activity都会有自己的生命周期, 有一系列方法会控制Activity的生命周期.如:onCreate(),o ...

  3. HDU 4869 Turn the pokers(思维+组合公式+高速幂)

    pid=4869" target="_blank">Turn the pokers 大意:给出n次操作,给出m个扑克.然后给出n个操作的个数a[i],每一个a[i] ...

  4. 39.Intellij导入子项目时,maven列表子项目灰色不可用---解决方法

    转自:https://blog.csdn.net/jackieriver/article/details/79046326 导入子项目的module时,左侧project目录中有一个module图标右 ...

  5. Django环境搭建(一)

    搭建Django环境之前先搭建python运行环境 需要了解: 解释器(编译器): 计算机不能直接理解任何除机器语言外的其他语言,所以程序员必须要把自己写的语言翻译成机器语言,而将其他语言翻译成机器语 ...

  6. Swift iOS tableView static cell动态计算高度

    TableView是iOS开发中经常使用的组件.有些表格由于UILabel包括的文本字数不一样,须要显示的高度也会不同,因此须要动态计算static cell的高度.我用的是static cell,注 ...

  7. 使用 STL 辅助解决算法问题

    不要重复制造轮子,而且你造的轮子未必比得上别人的: <numeric>⇒ accumulate,累积容器中区间的和,可以指定初值: 为什么 STL 中的容器和算法一定关于区间的操作一定是左 ...

  8. 每天自动备份MySQL数据库的shell脚本

    经常备份数据库是一个好习惯,虽然数据库损坏或数据丢失的概率很低,但一旦发生这种事情,后悔是没用的.一般网站或应用的后台都有备份数据库的功能按钮,但需要去手工执行.我们需要一种安全的,每天自动备份的方法 ...

  9. Android JavaMail介绍及发送一封简单邮件

    本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明.       JavaMail是SUN提供给开 ...

  10. RTC时钟和BKP的配置stm32

    摘自:https://blog.csdn.net/gtkknd/article/details/52233605 RTC和后备寄存器通过一个开关供电,在VDD有效的时候选择VDD供电,否则选择VBAT ...