{"id":25996,"date":"2020-11-11T00:00:00","date_gmt":"2020-11-10T23:00:00","guid":{"rendered":"https:\/\/blexin.com\/the-change-detection-in-angular\/"},"modified":"2021-05-20T18:21:47","modified_gmt":"2021-05-20T16:21:47","slug":"the-change-detection-in-angular","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/","title":{"rendered":"The change detection in Angular"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-attachment-id=\"25948\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25948\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&amp;ssl=1\" data-orig-size=\"1024,608\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"iamge00\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=300%2C178&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?resize=1024%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-25948\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When we talk about web solutions, change detection is a very important part of the framework that we are using because it\u2019s responsible for the DOM updates. We must not forget that it also deeply affects an application\u2019s performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Change detection has two main moments: tracking changes and rendering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s start from the rendering that is the process taking the internal state of the program (objects and arrays in Javascript) and projects it into something we can see on the screen(images, buttons and other visual elements).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the beginning, the rendering logic implementation does not seem too difficult but everything gets complicated when we take into account the time variable. The application state can change any time as a result of a user interaction or because data have arrived after an HTTP call.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" data-attachment-id=\"25950\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25950\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01.png?fit=1180%2C650&amp;ssl=1\" data-orig-size=\"1180,650\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image01\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01.png?fit=300%2C165&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01.png?fit=1024%2C564&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image01.png?resize=1024%2C564&#038;ssl=1\" alt=\"\" class=\"wp-image-25950\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-1024x564.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-980x540.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image01-480x264.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em><a href=\"https:\/\/indepth.dev\/what-every-front-end-developer-should-know-about-change-detection-in-angular-and-react\/\" target=\"_blank\" rel=\"noreferrer noopener\">Source<\/a><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a very interesting example at this&nbsp;<a href=\"https:\/\/stackblitz.com\/edit\/js-aufmae\" target=\"_blank\" rel=\"noreferrer noopener\">address<\/a>&nbsp;written in pure Javascript for a rating widget.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"128\" data-attachment-id=\"25952\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25952\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02.gif?fit=442%2C128&amp;ssl=1\" data-orig-size=\"442,128\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image02\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02.gif?fit=300%2C87&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02.gif?fit=442%2C128&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image02.gif?resize=442%2C128&#038;ssl=1\" alt=\"\" class=\"wp-image-25952\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Regardless of the initialisation code, we can write a class (let\u2019s call it RatingComponent) containing a property called rating whose value changes on the user selection. This change must trigger a DOM update. The rating setter is the right moment to trigger by code the change of involved css classes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nexport class RatingsComponent {\n  ...\n set rating(v) {\n this._rating = v;\n \n \/\/ triggers DOM update\n this.updateRatings();\n  }\n \n get rating() {\n return this._rating;\n  }\n \n updateRatings() {\n \/\/ Update DOM code\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This code does not scale with the growing complexity of the page and the conditional logic to be used in its elements. We must focus on the application logic and not on the screen updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s why we use a framework like Angular or libraries as React. Frameworks take care for us of synchronization between the internal state of the application and the user interfaces very efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How Angular implements the change detection?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the compiler analyzes the template, it identifies component\u2019s properties that are associated with DOM elements. For each association, the compiler creates a binding. A binding is a 1:1 correspondence between the involved property and the property of a DOM element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once bindings are created, Angular no longer works with templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The change detection mechanism executes instructions that process every binding. The job of these instructions is to check if the value of an expression with a component property has changed and perform DOM updates if necessary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take as example a possible implementation of the rating component, seen before.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;ul class=&quot;rating&quot; (click)=&quot;handleClick($event)&quot;&gt;\n    &lt;li &#x5B;className]=&quot;&#039;star &#039; + (rating &gt; 0 ? &#039;solid&#039; : &#039;outline&#039;)&quot;&gt;&lt;\/li&gt;\n    &lt;li &#x5B;className]=&quot;&#039;star &#039; + (rating &gt; 1 ? &#039;solid&#039; : &#039;outline&#039;)&quot;&gt;&lt;\/li&gt;\n    &lt;li &#x5B;className]=&quot;&#039;star &#039; + (rating &gt; 2 ? &#039;solid&#039; : &#039;outline&#039;)&quot;&gt;&lt;\/li&gt;\n    &lt;li &#x5B;className]=&quot;&#039;star &#039; + (rating &gt; 3 ? &#039;solid&#039; : &#039;outline&#039;)&quot;&gt;&lt;\/li&gt;\n    &lt;li &#x5B;className]=&quot;&#039;star &#039; + (rating &gt; 4 ? &#039;solid&#039; : &#039;outline&#039;)&quot;&gt;&lt;\/li&gt;\n&lt;\/ul&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In this example, the rating property in the template is bound to the className property through the expression:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;className]=&quot;&#039;star &#039; + ((ctx.rating &gt; 0) ? &#039;solid&#039; : &#039;outline&#039;)&quot;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The first binding created will be an object with a current value (for example \u2018outline\u2019) and a property called dirty set to false. When the rating is updated, binding will be marked as dirty equal to true and its value set to \u2018solid\u2019. The next instruction will check if the binding is dirty and, in that case, it uses the new value to update the Dom (the className).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are two ways to initiate the change detection. The first one is to invoke it manually from our code. The second one is to rely completely on the framework and let him run it automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the first case, we can inject the ChangeDetectorRef service (see&nbsp;<a href=\"https:\/\/angular.io\/api\/core\/ChangeDetectorRef\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>) in a component and call its detectChanges method in correspondence of an event.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the second case, nothing is added to our code. The question is therefore: how does the framework know when it should run the change detection?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s easy to intercept an event and schedule a change detection after the application code has been executed. The problem, again, is the asynchronous events like setTimeout or XHR, which are not managed by Angular.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To solve this problem, the framework uses an external library called zone.js that creates a wrapper around all the asynchronous events in the browser. Then, zone.js may notify Angular when a particular event happens.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make Zone available, you need to import the zone.js package. If you are using the command line interface, this step is done automatically, and you will see the following line in the src\/polyfills.ts:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/***************************************************************************************************\n * Zone JS is required by default for Angular itself.\n  *\/\n import &#039;zone.js\/dist\/zone&#039;; \/\/ Included with Angular CLI.\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">It\u2019s important to note that the zones are not part of the change detection mechanism in Angular. In fact, Angular can work without them as you can read&nbsp;<a href=\"https:\/\/indepth.dev\/do-you-still-think-that-ngzone-zone-js-is-required-for-change-detection-in-angular\/\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You just need to edit the main ts file responsible for the bootstrapping of our application<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nplatformBrowserDynamic()\n .bootstrapModule(AppModule, {\n ngZone: &#039;noop&#039;\n });\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The zones are an execution context for asynchronous operations. They turn out to be really useful to manage errors and make profiling. But what does that exactly mean?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to understand the part of the definition related to the execution context, we need to get a better picture of what problem the zones are trying to solve.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take as example 3 functions, executed in sequence:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nfunctionA();\nfunctionB();\nfunctionC();\n \nfunction functionA() {...}\nfunction functionB() {...}\nfunction functionC() {...}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say we want to measure the execution time of this code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;script&gt;\nconst t0 = performance.now();\nfunctionA();\nfunctionB();\nfunctionC();\nconst t1 = performance.now();\nconsole.log(Math.floor((t1-t0)*100) \/ 100 + &#039; ms&#039;);\n  \nfunction functionA() { console.log(&#039;function A&#039;);}\nfunction functionB() { console.log(&#039;function B&#039;);}\nfunction functionC() { console.log(&#039;function C&#039;);}\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"366\" data-attachment-id=\"25960\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25960\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?fit=448%2C366&amp;ssl=1\" data-orig-size=\"448,366\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image08\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?fit=300%2C245&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?fit=448%2C366&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?resize=448%2C366&#038;ssl=1\" alt=\"\" class=\"wp-image-25960\" srcset=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?w=448&amp;ssl=1 448w, https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image08.png?resize=300%2C245&amp;ssl=1 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">However, often we have asynchronous work to do. Asynchronous operations are ignored by our profile. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;script&gt;\nconst t0 = performance.now();\nfunctionA();\nfunctionB();\nfunctionC();\nconst t1 = performance.now();\nconsole.log(Math.floor((t1-t0)*100) \/ 100 + &#039; ms&#039;);\n  \nfunction functionA() { setTimeout(doSomething, 0);}\nfunction functionB() { setTimeout(doSomething, 0);}\nfunction functionC() { setTimeout(doSomething, 0);}\nfunction doSomething() {\n console.log(&#039;Async task&#039;);\n}\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">results in:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"180\" data-attachment-id=\"25963\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25963\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03.png?fit=530%2C180&amp;ssl=1\" data-orig-size=\"530,180\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image03\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03.png?fit=300%2C102&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03.png?fit=530%2C180&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image03.png?resize=530%2C180&#038;ssl=1\" alt=\"\" class=\"wp-image-25963\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image03.png 530w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image03-480x163.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 530px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">What happened with our code? Javascript is a single-threaded language. The asynchronous behaviour it\u2019s not part of the language itself but was added to it in the browser. The asynchronous features are accessible through the so called browser API.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"898\" data-attachment-id=\"25965\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25965\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04.png?fit=1118%2C980&amp;ssl=1\" data-orig-size=\"1118,980\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image04\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04.png?fit=300%2C263&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04.png?fit=1024%2C898&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image04.png?resize=1024%2C898&#038;ssl=1\" alt=\"\" class=\"wp-image-25965\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-1024x898.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-980x859.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image04-480x421.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/EventLoop\" target=\"_blank\" rel=\"noreferrer noopener\">Source<\/a><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objects are allocated in a Heap which is just a name to denote a large (mostly unstructured) region of memory, as shown in the image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Stack is the single thread that executes the Javascript code. Function calls forms a stack of frames. What is a frame? Let\u2019s come back to our example in the synchronous case and modify it slightly<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nfunction functionA(x) {\n let y = 3;\n return functionB(x * y);\n}\n  \nfunction functionB(b) {\n let a = 10;\n return a + b + 11;\n}\nconsole.log(functionA(5));\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When calling functionA, a first frame is created containing functionA\u2019s arguments and local variables. When functionA calls functionB, a second frame is created and pushed on top of the first one containing functionB&#8217;s arguments and local variables. When functionB returns, the top frame element is popped out of the stack (leaving only functionA&#8217;s call frame). When functionA returns, the stack is empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what is the Queue shown in the picture? Let\u2019s take this example introducing a setTimeout call.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nfunction main() {\n console.log(&#039;A&#039;);\n setTimeout( function exec() {\n console.log(&#039;B&#039;);\n }, 0);\n}\n  \nmain();\n\/\/ Output\n\/\/ A\n\/\/ C\n\/\/ B\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">setTimeout() has a delay of 0 ms and a callback function called exec(). setTimeout is pushed in the stack and its execution starts. But setTimeout belongs to the browser API then the responsibility to start the exec() function is delegated to the browser APIs. Meanwhile, setTimeout() is removed from the stack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After 0 ms wait, browser APIs cannot push the exec function directly to the stack. What they can do is put the exec() function in a message queue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only when the stack is empty (frameless), the first item queued in the Message Queue can be pushed to the stack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Meanwhile, in the stack was pushed the console.log(\u2018C\u2019) instruction. This print will precede the one produced by exec():<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The 0 that is in setTimeout() often causes confusion. It is never 0 but rather the minimum waiting time after which the callback function will run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we insert a blocking statement, the emptying of the queue will be further delayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I recommend you take a look at&nbsp;<a href=\"http:\/\/latentflip.com\/loupe\/\" target=\"_blank\" rel=\"noreferrer noopener\">this web page<\/a>&nbsp;containing a fantastic visual representation of what we have just explained. It is also possible to modify the code.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"537\" data-attachment-id=\"25968\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25968\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05.png?fit=2812%2C1474&amp;ssl=1\" data-orig-size=\"2812,1474\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image05\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05.png?fit=300%2C157&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05.png?fit=1024%2C537&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image05.png?resize=1024%2C537&#038;ssl=1\" alt=\"\" class=\"wp-image-25968\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-980x514.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image05-480x252.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s go back to the problem of profiling our code in presence of asynchronous code. What we need are basically hooks that allow us to execute some profiling code whenever such an asynchronous task happens.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is where zones come into play. Zones can perform an operation (for example, starting or stopping a timer, or saving a stack trace) whenever that code enters or exits a zone. They can override methods within our code, or even associate data with individual zones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coming back to Angular, when an application is bootstrapped a zone is created called Ngzone. This is the zone that the Angular application runs in. And the framework only gets notifications about events that occur inside this zone.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"613\" data-attachment-id=\"25971\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25971\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06.png?fit=1282%2C768&amp;ssl=1\" data-orig-size=\"1282,768\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image06\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06.png?fit=300%2C180&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06.png?fit=1024%2C613&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image06.png?resize=1024%2C613&#038;ssl=1\" alt=\"\" class=\"wp-image-25971\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-980x587.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image06-480x288.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Angular provides an ngZone service that can be injected in our application and that can be used to fork the Angular zone if we want to execute code outside of it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">More generally, the NgZone service provides us with a number of Observables and methods for determining the state of Angular&#8217;s zone and to execute code in different ways inside and outside Angular&#8217;s zone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can see a simple&nbsp;<a href=\"https:\/\/indepth.dev\/a-gentle-introduction-into-change-detection-in-angular\/\" target=\"_blank\" rel=\"noreferrer noopener\">example<\/a>&nbsp;where it\u2019s necessary to use this service.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take the following code with its template.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nexport class AppComponent {\n get time() {\n return Date.now();\n  }\n}\n \n&lt;h3&gt;\n Change detection is triggered at:\n     &lt;span &#x5B;textContent]=&quot;time | date:&#039;hh:mm:ss:SSS&#039;&quot;&gt;&lt;\/span&gt;\n&lt;\/h3&gt;\n&lt;button (click)=&quot;0&quot;&gt;Trigger Change Detection&lt;\/button&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This code generates an error in the console!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"594\" data-attachment-id=\"25974\" data-permalink=\"https:\/\/blexin.com\/en\/?attachment_id=25974\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07.png?fit=1378%2C800&amp;ssl=1\" data-orig-size=\"1378,800\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image07\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07.png?fit=300%2C174&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07.png?fit=1024%2C594&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/image07.png?resize=1024%2C594&#038;ssl=1\" alt=\"\" class=\"wp-image-25974\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-980x569.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/image07-480x279.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This is an error that has happened to anyone who has ever worked with Angular as you can read on the repository issues&nbsp;<a href=\"https:\/\/github.com\/angular\/angular\/issues\/17572\" target=\"_blank\" rel=\"noreferrer noopener\">page<\/a>&nbsp;on github. Although the long name explains exactly what happened the first reaction is definitely a desperate search on StackOverflow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When Angular creates the DOM nodes to render the contents of the template of a component on the screen, it needs a place to store the references to those DOM nodes. For that purpose, internally there\u2019s a data structure known as View. It\u2019s also used to store the reference to the component instance and the previous values of binding expressions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As said before, change detection is performed for each component. Now that we know that components are represented internally as views, we can say that the change detection is performed for each view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a view is checked, Angular runs over all bindings generated for a view by the compiler. It evaluates expressions and compares their result to the values stored in the oldValues array on the view. If it detects a difference, it updates the DOM property relevant to the binding and the oldValues array on the view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After each change detection cycle, in development mode, Angular synchronously runs another check to ensure that expressions produce the same values as during the previous change detection cycle. This check doesn\u2019t update the DOM but throws our nice exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why execute a new cycle of checks?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, imagine that some properties of components have been updated during the change detection execution. As a result, expressions produce new values that are inconsistent with what\u2019s rendered on the screen. Angular certainly could run another change detection cycle but it\u2019s evident it could actually end up in an infinite loop of change detection runs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid this situation, Angular imposed the so-called Unidirectional Data Flow. Once Angular has processed bindings for the current component, you can no longer update its properties that are used in expressions for bindings. The raising of the ExpressionChangedAfterItHasBeenCheckedError exception is the mechanism to implement the data flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ok, we just need another setInterval to trigger a new change detection cycle. Wrong! Again we will produce an infinite cycle of change detections.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution consists in executing the update of the time property in a zone different from the one of Angular. How can we make it? The first idea that comes to mind is using a setinterval in this zone.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nexport class AppComponent {\n mytime: number;\n  \n get time(): number {\n return this.mytime;\n  }\n  \nconstructor(private zone: NgZone) {\n this.mytime = Date.now();\n  \n this.zone.runOutsideAngular(() =&gt; {\n setInterval(() =&gt; {\n this.mytime = Date.now();\n }, 0);\n });\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Please remember that the use of setinterval has to be deprecated in the real code you bring to production.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can choose instead to use the onPush strategy on the component (ChangeDetectionStrategy.onPush), to prevent Angular from choosing when to run the change detection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With onPush the change detection will kicks in when:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The Input reference changes;<\/li><li>An event originated from the component or one of its children;<\/li><li>Run change detection explicitly (componentRef.markForCheck());<\/li><li>Use the async pipe in the view.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">I hope that you have been interested in this subject and that it has shed some light on an extremely complex subject.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See you next!<\/p>\n\n\n[et_pb_section global_module=\"26155\"][\/et_pb_section]\n\n\n[et_pb_section global_module=\"26266\"][\/et_pb_section]\n","protected":false},"excerpt":{"rendered":"<p>What lies behind rendering in a web browser<\/p>\n","protected":false},"author":196716245,"featured_media":25948,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[688637524],"tags":[688637390,688637391,688637392],"class_list":["post-25996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-angular-en","tag-javascript-en","tag-web-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The change detection in Angular - Blexin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The change detection in Angular - Blexin\" \/>\n<meta property=\"og:description\" content=\"What lies behind rendering in a web browser\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-10T23:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-20T16:21:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Salvatore Sorrentino\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Salvatore Sorrentino\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\"},\"author\":{\"name\":\"Salvatore Sorrentino\",\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed\"},\"headline\":\"The change detection in Angular\",\"datePublished\":\"2020-11-10T23:00:00+00:00\",\"dateModified\":\"2021-05-20T16:21:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\"},\"wordCount\":1934,\"image\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1\",\"keywords\":[\"Angular\",\"Javascript\",\"Web\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\",\"url\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\",\"name\":\"The change detection in Angular - Blexin\",\"isPartOf\":{\"@id\":\"https:\/\/blexin.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1\",\"datePublished\":\"2020-11-10T23:00:00+00:00\",\"dateModified\":\"2021-05-20T16:21:47+00:00\",\"author\":{\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed\"},\"breadcrumb\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1\",\"width\":1024,\"height\":608},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blexin.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The change detection in Angular\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blexin.com\/en\/#website\",\"url\":\"https:\/\/blexin.com\/en\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blexin.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed\",\"name\":\"Salvatore Sorrentino\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g\",\"caption\":\"Salvatore Sorrentino\"},\"url\":\"https:\/\/blexin.com\/en\/author\/salvatore-sorrentinoblexin-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The change detection in Angular - Blexin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/","og_locale":"en_US","og_type":"article","og_title":"The change detection in Angular - Blexin","og_description":"What lies behind rendering in a web browser","og_url":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/","og_site_name":"Blexin","article_published_time":"2020-11-10T23:00:00+00:00","article_modified_time":"2021-05-20T16:21:47+00:00","og_image":[{"width":1024,"height":608,"url":"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","type":"image\/png"}],"author":"Salvatore Sorrentino","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Salvatore Sorrentino","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/"},"author":{"name":"Salvatore Sorrentino","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed"},"headline":"The change detection in Angular","datePublished":"2020-11-10T23:00:00+00:00","dateModified":"2021-05-20T16:21:47+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/"},"wordCount":1934,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","keywords":["Angular","Javascript","Web"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/","url":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/","name":"The change detection in Angular - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","datePublished":"2020-11-10T23:00:00+00:00","dateModified":"2021-05-20T16:21:47+00:00","author":{"@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed"},"breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","width":1024,"height":608},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/the-change-detection-in-angular\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"The change detection in Angular"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/354db2bc97cac71c2ceeca21a92d5bed","name":"Salvatore Sorrentino","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a6ec0dc827d2acaa55df9dbf1007e23f6fcb8c9436df52ab48274bb2221085bf?s=96&d=identicon&r=g","caption":"Salvatore Sorrentino"},"url":"https:\/\/blexin.com\/en\/author\/salvatore-sorrentinoblexin-com\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/iamge00.png?fit=1024%2C608&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-6Li","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/25996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/users\/196716245"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=25996"}],"version-history":[{"count":18,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/25996\/revisions"}],"predecessor-version":[{"id":31810,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/25996\/revisions\/31810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/25948"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=25996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=25996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=25996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}