Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Today's Schedule 1. zone.js 2. Change Detection 3. Content Projection

Slide 3

Slide 3 text

How does angular know when to update the bindings? https://github.com/angular/zone.js/

Slide 4

Slide 4 text

zone.js • Monkey patches the world - every single async api • Provides context The zone is an execution context that is passed to the callback function of the async operation and when the callback is executed the zone is alerted about the change.
 ($digest) https://github.com/angular/zone.js/blob/master/lib/browser/browser.ts

Slide 5

Slide 5 text

zone.js Usually we don't need to care about zones but there are times when we need to like when we are doing a complex animation or third party services

Slide 6

Slide 6 text

Change Detection

Slide 7

Slide 7 text

Change Detection Change Detection mechanism detects when data changes, and then automatically re-renders the view to reflect that change.

Slide 8

Slide 8 text

Application State Change • Events - click, submit, … • XHR (Ajax) - Fetching data from a remote server • Timers - setTimeout(), setInterval()

Slide 9

Slide 9 text

Change Detection Mechanism Every Component has an change detector and what it does is: 1. When a change occurs the change detector compares for each expression in the template the current value of the property used in the expression with the previous value. 2. If the property value before and after is different, it sets isChanged to true.

Slide 10

Slide 10 text

Change Detection

Slide 11

Slide 11 text

Change Detection

Slide 12

Slide 12 text

Change Detection

Slide 13

Slide 13 text

Change Detection

Slide 14

Slide 14 text

How fast is the Change Detection?

Slide 15

Slide 15 text

Change Detection Time = C * TotalNumberOfBindings C - average time to check a binding

Slide 16

Slide 16 text

Angular 2 generates Virtual Machine (VM) friendly code so it can perform thousands of Change Detection Runs in milliseconds.

Slide 17

Slide 17 text

How to optimize the
 Change Detection Traversal ?

Slide 18

Slide 18 text

Change Detection Strategies • ChangeDetectionStrategy.OnPush 
 The change detector's mode will be set to CheckOnce during hydration (It compares values by reference) • ChangeDetectionStrategy.Default 
 The change detector's mode will be set to CheckAlways during hydration

Slide 19

Slide 19 text

https://plnkr.co/edit/4HG4sDcdTleRXlNafETH?p=preview

Slide 20

Slide 20 text

@ContentChild @ContentChildren Searching for components or elements in located between the closing and opening tags of component / directive.

Slide 21

Slide 21 text

Content Projection Content projection is a way to import HTML content from outside the component and insert that content into the component's template in a designated spot. 


Slide 22

Slide 22 text

Multi Slot Content Projection For having more control over content projection

Slide 23

Slide 23 text

@ViewChild @ViewChildren Searching for components or elements in the template of the Component.

Slide 24

Slide 24 text

AfterViewInit and AfterContentInit Hooks • The AfterView hooks concern ViewChildren, the child components whose element tags appear within the component's template. • The AfterContent hooks concern ContentChildren, the child components that Angular projected into the component.

Slide 25

Slide 25 text

Questions?