Angular 2.0 change detection

21
Angular 2.0 Change detection RAN WAHLE

Transcript of Angular 2.0 change detection

Page 1: Angular 2.0 change detection

Angular 2.0 Change detectionRAN WAHLE

Page 2: Angular 2.0 change detection

Agenda

A little about Angular 1.x The Angular 2.0 component change detector Angular and zone Immutables Observables Summary

Page 3: Angular 2.0 change detection

A little bit about me

(Soon to be) Javascript developer @Tikal Consultant, speaker Volunteer in several programming projects Not a UI person

Page 4: Angular 2.0 change detection

AngularJs 1.x scopes tree

Page 5: Angular 2.0 change detection

Angular 1.x change detection

Page 6: Angular 2.0 change detection

Angular 1.x summary

Prototypical inheritance of scopes Generic mechanism for change detection Change detection is not predictable

Page 7: Angular 2.0 change detection

Angular 2.0 change detection goals

Performance Easy to use Transparency

Page 8: Angular 2.0 change detection

Angular 2.0 change detection

Predictable Tree instead of graph

Page 9: Angular 2.0 change detection

Angular 2.0 events

Zone.js Overrides web api for addEventListener, XHR, Timers, WebSockets

and more… Overriding will

Assign the callback Run change detection Update the UI

Page 10: Angular 2.0 change detection

Change detection tree@component

@component@component

@component@component

@component

@component@component

@component

Page 11: Angular 2.0 change detection

Change detector

Each component has its own change detector, created in compilation time

It may be found under <ComponentClassName>.ngfactory It has a detectChangeInternal method tailored to its data Its detectChangeInternal calls detectContentChildrenChanges

and detectViewChildrenChanges

Page 12: Angular 2.0 change detection

Change detector Demo

Page 13: Angular 2.0 change detection

How a change is determined

Javascript objects are mutable The default change detector, detect a reference change

myObj = new {prop: ‘value’}

myObj.prop =‘otherValue’;

Page 14: Angular 2.0 change detection

OnPush strategy

There are two change detection strategies Default will trigger change detection whenever a bounded object is

changed OnPush will trigger change detection whenever an @Input decorated

object is changed.

Page 15: Angular 2.0 change detection

ImmutableJs

Javascript objects are mutable Whenever an object property change,

Change detection will run on its bounded change detectorand down

If we like to effect its parent change detector we need events

Page 16: Angular 2.0 change detection

ImmutableJs (cont.)

Immutable objects cannot be internally changed once created To “change” Immutable object we need to create a new one By that way we can easily “undo” changes ImmutableJs enables as to create this immutable objects

Page 17: Angular 2.0 change detection

ImmutableJs Demo

Page 18: Angular 2.0 change detection

Manually control change detection

At the component constructor we can get ChangeDetectorRef That’s acually the code generated for the component. Through this object we can control change detection

Page 19: Angular 2.0 change detection

ChangeDetectorRef Demo

Page 20: Angular 2.0 change detection

Summary

Change detection in Angular 2.0 is more efficient, transparent and easy to use

Its compiler generates change detector specifically to our components. Change detection goes top-down. OnPush strategy limits the change detection to the input members Immutable & observable objects enables as wider change detection We can also control change detection manually

Page 21: Angular 2.0 change detection

Thank you!

@ranwahle https://blogs.microsoft.co.il/ranw [email protected] https://www.linkedin.com/in/ranwahle