Slide 1

Slide 1 text

Introduction to Angular 2 Minko Gechev github.com/mgechev twitter.com/mgechev blog.mgechev.com

Slide 2

Slide 2 text

Minko Gechev github.com/mgechev twitter.com/mgechev { "job": "Freelancer", "hobbies": [ "open source", "blogging", "teaching", "sports" ], "communityEvents": [ "SofiaJS", "BeerJS Sofia" ] }

Slide 3

Slide 3 text

Angular 2 (quick recap)

Slide 4

Slide 4 text

– angular.io “Angular is a development platform for building mobile and desktop web applications”

Slide 5

Slide 5 text

Client Server

Slide 6

Slide 6 text

Client Server GET

Slide 7

Slide 7 text

Client Server GET

Slide 8

Slide 8 text

Client Server GET POST

Slide 9

Slide 9 text

Client Server GET POST

Slide 10

Slide 10 text

Client Server GET POST Angular 2

Slide 11

Slide 11 text

Written in AtSc…TypeScript?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

TypeScript Superset of JavaScript • Strongly & statically typed • By Microsoft

Slide 14

Slide 14 text

Component based

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

List Post Vote Label Button Label

Slide 18

Slide 18 text

List Post Vote Label Button Label

Slide 19

Slide 19 text

One-way Data Flow

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

A B C D E F

Slide 23

Slide 23 text

Based on the Web Standards

Slide 24

Slide 24 text

Web Components

Slide 25

Slide 25 text

New Router

Slide 26

Slide 26 text

Angular 1 Router

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Real Modules

Slide 29

Slide 29 text

Angular 1 Modules

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No More $scope.$apply

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Just to make sure we’re on the same page…

Slide 34

Slide 34 text

ECMAScript 5 Property Descriptors (quick recap)

Slide 35

Slide 35 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 36

Slide 36 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 37

Slide 37 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 38

Slide 38 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 39

Slide 39 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 40

Slide 40 text

ECMAScript 5 Property Descriptor Object.defineProperty(obj, 'name', { set: function (val) { console.log('Setting the value'); this._name = val; }, get: function (val) { console.log('Getting the value'); return this._name; } }); obj.name = 'foo'; console.log(obj.name); // Setting the value // Getting the value // foo

Slide 41

Slide 41 text

ECMAScript 2015 (quick recap)

Slide 42

Slide 42 text

“ES2015 classes are a simple sugar over the prototype- based OO pattern…” Classes

Slide 43

Slide 43 text

ECMAScript 6 Classes class Developer extends Person { constructor(name, language) { super(name); this._language = language; } get language() { return this._language; } set language(language) { this.doSomethingElse(); this._language = language; } talk() { super.talk(); console.log(`I'm a professional ${this.language} developer`); } }

Slide 44

Slide 44 text

ECMAScript 6 Classes class Developer extends Person { constructor(name, language) { super(name); this._language = language; } get language() { return this._language; } set language(language) { this.doSomethingElse(); this._language = language; } talk() { super.talk(); console.log(`I'm a professional ${this.language} developer`); } }

Slide 45

Slide 45 text

ECMAScript 6 Classes class Developer extends Person { constructor(name, language) { super(name); this._language = language; } get language() { return this._language; } set language(language) { this.doSomethingElse(); this._language = language; } talk() { super.talk(); console.log(`I'm a professional ${this.language} developer`); } }

Slide 46

Slide 46 text

ECMAScript 6 Classes class Developer extends Person { constructor(name, language) { super(name); this._language = language; } get language() { return this._language; } set language(language) { this.doSomethingElse(); this._language = language; } talk() { super.talk(); console.log(`I'm a professional ${this.language} developer`); } }

Slide 47

Slide 47 text

ECMAScript 6 Classes class Developer extends Person { constructor(name, language) { super(name); this._language = language; } get language() { return this._language; } set language(language) { this.doSomethingElse(); this._language = language; } talk() { super.talk(); console.log(`I'm a professional ${this.language} developer`); } }

Slide 48

Slide 48 text

“Arrows are a function shorthand using the => syntax…Unlike functions, arrows share the same lexical this as their surrounding code.” Arrow Functions

Slide 49

Slide 49 text

“Arrows are a function shorthand using the => syntax…Unlike functions, arrows share the same lexical this as their surrounding code.” Arrow Functions

Slide 50

Slide 50 text

Arrow Functions [1, 2, 3, 4].reduce((a, c) => a + c, 0); [1, 2, 3].map(c => c + 1); function Developer() { this.talk = () => { console.log(`I speak ${this._language} fluently`); }; }

Slide 51

Slide 51 text

Arrow Functions [1, 2, 3, 4].reduce((a, c) => a + c, 0); [1, 2, 3].map(c => c + 1); function Developer() { this.talk = () => { console.log(`I speak ${this._language} fluently`); }; }

Slide 52

Slide 52 text

Arrow Functions [1, 2, 3, 4].reduce((a, c) => a + c, 0); [1, 2, 3].map(c => c + 1); function Developer() { this.talk = () => { console.log(`I speak ${this._language} fluently`); }; }

Slide 53

Slide 53 text

Arrow Functions [1, 2, 3, 4].reduce((a, c) => a + c, 0); [1, 2, 3].map(c => c + 1); function Developer() { this.talk = () => { console.log(`I speak ${this._language} fluently`); }; }

Slide 54

Slide 54 text

Let “Let allows block scope definition of variables.”

Slide 55

Slide 55 text

Functional lexical scope for (var i = 0; i < 10; i += 1) { var foo = i + 1; } console.log(typeof i); // number console.log(typeof foo); // number

Slide 56

Slide 56 text

Local lexical scope for (let i = 0; i < 10; i += 1) { let foo = i + 1; } console.log(typeof i); // undefined console.log(typeof foo); // undefined

Slide 57

Slide 57 text

Promises “Promises could be used instead of callbacks for handling asynchronous actions. They are used as containers for resolving “future” values.”

Slide 58

Slide 58 text

Promises fetch('/data.json') .then(data => { return data.json(); }, error => { console.error(error); }) .then(json => doStuff(json));

Slide 59

Slide 59 text

Promises Promise.all([ fetch('/a.json'), fetch('/b.json'), fetch('/c.json') ]).then(res => { return Promise.all([ res[0].text(), res[1].text(), res[2].text() ]); }) .then(data => { console.log(data.join('')); });

Slide 60

Slide 60 text

Modules “Language-level support for modules for component definition.”

Slide 61

Slide 61 text

Modules “Language-level support for modules for component definition.”

Slide 62

Slide 62 text

Modules // Developer.js export default class Developer { // ... } // app.js import Developer from './Developer'; // Math.js let log2 = Math.log2.bind(Math); let sin = Math.sin.bind(Math); export { log2, sin }; // app.js import * as math from './Math'; math.log2(10);

Slide 63

Slide 63 text

Modules // Developer.js export default class Developer { // ... } // app.js import Developer from './Developer'; // Math.js let log2 = Math.log2.bind(Math); let sin = Math.sin.bind(Math); export { log2, sin }; // app.js import * as math from './Math'; math.log2(10);

Slide 64

Slide 64 text

Modules // Developer.js export default class Developer { // ... } // app.js import Developer from './Developer'; // Math.js let log2 = Math.log2.bind(Math); let sin = Math.sin.bind(Math); export { log2, // same as log2: log2 sin }; // app.js import * as math from './Math'; math.log2(10);

Slide 65

Slide 65 text

Module Loaders “Additional imperative API to the declarative module syntax, which allows you to programmatically work with modules and configure the module loading.”

Slide 66

Slide 66 text

Module Loaders System.import('/app') .then(m => m.init(), e => console.error(e));

Slide 67

Slide 67 text

ECMAScript 2016

Slide 68

Slide 68 text

– github.com/wycats/javascript-decorators “Decorators make it possible to annotate and modify classes and properties at design time.” ES2016 Decorators

Slide 69

Slide 69 text

Decorators @Aspect class LoggingAspect { @Before(/.*/, /.*/) before(args) { console.log(`${args.name} called with ${args.args}`); } @After(/.*/, /.*/) after(args) { console.log(`${args.name} execution completed`); } @OnThow(/.*/, /.*/) errorHandler(args) { console.error(`${args.name} threw an error`, args.error); } }

Slide 70

Slide 70 text

Decorators @Aspect class LoggingAspect { @Before(/.*/, /.*/) before(args) { console.log(`${args.name} called with ${args.args}`); } @After(/.*/, /.*/) after(args) { console.log(`${args.name} execution completed`); } @OnThow(/.*/, /.*/) errorHandler(args) { console.error(`${args.name} threw an error`, args.error); } }

Slide 71

Slide 71 text

TypeScript (quick recap)

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

TypeScript • Superset of ECMAScript • Allows optional static typing • Better support by text editors/IDEs • Reduce bugs • Faster code

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 77

Slide 77 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 78

Slide 78 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 79

Slide 79 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 80

Slide 80 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 81

Slide 81 text

TypeScript Class class Developer { age:string; constructor(private languages:string[], private name:string, age) { this.age = (age).toString(2); } private codeInPHP() { // body } public writeSoftware() { // body } public coffeeBreak(nextTask:{(name:string):void}) { setTimeout(nextTask, 1000); } }

Slide 82

Slide 82 text

Interfaces

Slide 83

Slide 83 text

Interfaces interface A { foo():void; bar(a:number):string; } interface B { baz(a:string):string } interface C extends A, B { } class D implements C { // ... }

Slide 84

Slide 84 text

Interfaces interface A { foo():void; bar(a:number):string; } interface B { baz(a:string):string } interface C extends A, B { } class D implements C { // ... }

Slide 85

Slide 85 text

Interfaces interface A { foo():void; bar(a:number):string; } interface B { baz(a:string):string } interface C extends A, B { } class D implements C { // ... }

Slide 86

Slide 86 text

Interfaces interface A { foo():void; bar(a:number):string; } interface B { baz(a:string):string } interface C extends A, B { } class D implements C { // ... }

Slide 87

Slide 87 text

Type Inference

Slide 88

Slide 88 text

Type Inference let answer = 42; let callback:{(a:number, ...nums:any[]):number}; callback = (a, nums) => nums.reduce((a, c) => a + c, a); let foo; foo = 42;

Slide 89

Slide 89 text

Type Inference let answer = 42; let callback:{(a:number, ...nums:any[]):number}; callback = (a, nums) => nums.reduce((a, c) => a + c, a); let foo; foo = 42;

Slide 90

Slide 90 text

Type Inference let answer = 42; let callback:{(a:number, ...nums:any[]):number}; callback = (a, nums) => nums.reduce((a, c) => a + c, a); let foo; foo = 42;

Slide 91

Slide 91 text

Type Inference let answer = 42; let callback:{(a:number, ...nums:any[]):number}; callback = (a, nums) => nums.reduce((a, c) => a + c, a); let foo; foo = 42;

Slide 92

Slide 92 text

Adopting Existing Libraries

Slide 93

Slide 93 text

Ambient Declarations interface JQueryStatic { /** * Perform an asynchronous HTTP (Ajax) request. * * @param settings A set of key/value pairs that … */ ajax(settings: JQueryAjaxSettings): JQueryXHR; /** * Perform an asynchronous HTTP (Ajax) request. * * @param url A string containing the URL to which the request is sent. * @param settings A set of key/value pairs that… */ ajax(url: string, settings?: JQueryAjaxSettings): JQueryXHR; // ... } declare var jQuery: JQueryStatic; declare var $: JQueryStatic;

Slide 94

Slide 94 text

Add Type Definition Reference /// let elems = $('.elems'); //...

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Main Concepts

Slide 97

Slide 97 text

Annotations

Slide 98

Slide 98 text

Annotations import {Component, View, NgFor} from 'angular2/angular2'; import {Home} from './components/home/home'; import {About} from './components/about/about'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html?v=<%= VERSION %>', directives: [RouterOutlet, RouterLink] }) class App {}

Slide 99

Slide 99 text

Annotations import {Component, View, NgFor} from 'angular2/angular2'; import {Home} from './components/home/home'; import {About} from './components/about/about'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html?v=<%= VERSION %>', directives: [RouterOutlet, RouterLink] }) class App {}

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

ES7 Decorators != Angular Annotations

Slide 102

Slide 102 text

ES7 Decorators != Angular Annotations Annotations are implemented with decorators

Slide 103

Slide 103 text

Directives

Slide 104

Slide 104 text

Directives A few directives… • ng-for • ng-if • ng-switch • ng-style

Slide 105

Slide 105 text

ng-if

Slide 106

Slide 106 text

ng-for {{user}}

Slide 107

Slide 107 text

Directives Components

Slide 108

Slide 108 text

UserDetails.ts import {Component, View} from ‘angular2/angular2'; import {NgIf} from 'angular2/angular2'; @Component({ selector: 'user-details', properties: ['user'] }) @View({ templateUrl: ‘./user-details.html’, directives: [NgIf] }) export class UserDetails { user:any; constructor() {} }

Slide 109

Slide 109 text

UserDetails.ts import {Component, View} from ‘angular2/angular2'; import {NgIf} from 'angular2/angular2'; @Component({ selector: 'user-details', properties: ['user'] }) @View({ templateUrl: ‘./user-details.html’, directives: [NgIf] }) export class UserDetails { user:any; constructor() {} }

Slide 110

Slide 110 text

UserDetails.ts import {Component, View} from ‘angular2/angular2'; import {NgIf} from 'angular2/angular2'; @Component({ selector: 'user-details', properties: ['user'] }) @View({ templateUrl: ‘./user-details.html’, directives: [NgIf] }) export class UserDetails { user:any; constructor() {} }

Slide 111

Slide 111 text

Services

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Service.ts @Injectable() export class Http { constructor(private _backend: XHRBackend, private _defaultOptions: BaseRequestOptions) {} get(url: string, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({method: RequestMethods.GET}))); } post(url: string, body: any, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({body: body, method: RequestMethods.POST}))); } }

Slide 114

Slide 114 text

Service.ts @Injectable() export class Http { constructor(private _backend: XHRBackend, private _defaultOptions: BaseRequestOptions) {} get(url: string, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({method: RequestMethods.GET}))); } post(url: string, body: any, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({body: body, method: RequestMethods.POST}))); } }

Slide 115

Slide 115 text

Service.ts @Injectable() export class Http { constructor(private _backend: XHRBackend, private _defaultOptions: BaseRequestOptions) {} get(url: string, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({method: RequestMethods.GET}))); } post(url: string, body: any, options?: IRequestOptions) { return httpRequest(this._backend, new Request(url, this._defaultOptions.merge(options) .merge({body: body, method: RequestMethods.POST}))); } }

Slide 116

Slide 116 text

Change Detection

Slide 117

Slide 117 text

A B E D C

Slide 118

Slide 118 text

A B E D C

Slide 119

Slide 119 text

A B E D C

Slide 120

Slide 120 text

A B E D C

Slide 121

Slide 121 text

Detection mechanisms • No front-end state • Event based • Difference based • Dirty checking

Slide 122

Slide 122 text

No front-end state

Slide 123

Slide 123 text

Server

Slide 124

Slide 124 text

Server

Slide 125

Slide 125 text

Server

Slide 126

Slide 126 text

Event Based

Slide 127

Slide 127 text

A B E D C

Slide 128

Slide 128 text

A B E D C Change Event

Slide 129

Slide 129 text

A B E D C

Slide 130

Slide 130 text

Difference Based

Slide 131

Slide 131 text

A B E D C Page Body Header Content Sidebar

Slide 132

Slide 132 text

A B E D C Page Body Header Content Sidebar

Slide 133

Slide 133 text

A B E D C Page Body Header Content Sidebar Page Body Header Content Sidebar

Slide 134

Slide 134 text

A B E D C

Slide 135

Slide 135 text

A B E D C Page Body Header Content Sidebar

Slide 136

Slide 136 text

Dirty Checking

Slide 137

Slide 137 text

{{user.name}}

Slide 138

Slide 138 text

addWatcher({ last: eval('user.name'); exp: 'user.name', fn: result => { element.text(result); } }); {{user.name}}

Slide 139

Slide 139 text

Dirty Checking let digest = (component) => { let dirty, watcher, current, i; let { watchers, children } = component; do { dirty = false; for (i = 0; i < watchers.length; i++) { watcher = watchers[i]; current = eval(watcher.exp); if (!equals(watcher.last, current)) { watcher.last = clone(current); dirty = true; watcher.fn(current); } } } while (dirty); for (i = 0; i < children.length; i++) digest(children[i]); };

Slide 140

Slide 140 text

Dirty Checking let digest = (component) => { let dirty, watcher, current, i; let { watchers, children } = component; do { dirty = false; for (i = 0; i < watchers.length; i++) { watcher = watchers[i]; current = eval(watcher.exp); if (!equals(watcher.last, current)) { watcher.last = clone(current); dirty = true; watcher.fn(current); } } } while (dirty); for (i = 0; i < children.length; i++) digest(children[i]); };

Slide 141

Slide 141 text

Dirty Checking let digest = (component) => { let dirty, watcher, current, i; let { watchers, children } = component; do { dirty = false; for (i = 0; i < watchers.length; i++) { watcher = watchers[i]; current = eval(watcher.exp); if (!equals(watcher.last, current)) { watcher.last = clone(current); dirty = true; watcher.fn(current); } } } while (dirty); for (i = 0; i < children.length; i++) digest(children[i]); };

Slide 142

Slide 142 text

Dirty Checking let digest = (component) => { let dirty, watcher, current, i; let { watchers, children } = component; do { dirty = false; for (i = 0; i < watchers.length; i++) { watcher = watchers[i]; current = eval(watcher.exp); if (!equals(watcher.last, current)) { watcher.last = clone(current); dirty = true; watcher.fn(current); } } } while (dirty); for (i = 0; i < children.length; i++) digest(children[i]); };

Slide 143

Slide 143 text

Dirty Checking let digest = (component) => { let dirty, watcher, current, i; let { watchers, children } = component; do { dirty = false; for (i = 0; i < watchers.length; i++) { watcher = watchers[i]; current = eval(watcher.exp); if (!equals(watcher.last, current)) { watcher.last = clone(current); dirty = true; watcher.fn(current); } } } while (dirty); for (i = 0; i < children.length; i++) digest(children[i]); };

Slide 144

Slide 144 text

List Post Vote Label Button Label

Slide 145

Slide 145 text

List Post Vote Label Button Label Click!

Slide 146

Slide 146 text

List Post Vote Label Button Label

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

Not exactly…

Slide 149

Slide 149 text

List Post Vote Label Button Label Click!

Slide 150

Slide 150 text

List Post Vote Label Button Label

Slide 151

Slide 151 text

What about two-way data binding?

Slide 152

Slide 152 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val; });

Slide 153

Slide 153 text

What if…

Slide 154

Slide 154 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val; });

Slide 155

Slide 155 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val + 1; });

Slide 156

Slide 156 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val + 1; });

Slide 157

Slide 157 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val + 1; });

Slide 158

Slide 158 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val + 1; });

Slide 159

Slide 159 text

Two-way binding input.onchange = () => { component[property] = input.value; digest(); }; addWatcher(property, (val) => { input.value = val + 1; });

Slide 160

Slide 160 text

No two-way data binding

Slide 161

Slide 161 text

Zone

Slide 162

Slide 162 text

The dirty checking should be run on any change

Slide 163

Slide 163 text

Zone monkey patches standard async APIs

Slide 164

Slide 164 text

Zone helps us keep track of the changes

Slide 165

Slide 165 text

unbuffer x | unbuffer command done find echo echo Pipes find ls ls command # process each line, using variables as parsed into $var1, $var2, etc # (note that this may be a subshell: var1, var2 etc will not be available # after the while loop terminates; some shells, such as zsh and newer # versions of Korn shell, process the commands to the left of the pipe # operator in a subshell) done

Slide 166

Slide 166 text

Pipe Interface export interface Pipe { supports(obj): boolean; onDestroy(): void; transform(value: any): any; }

Slide 167

Slide 167 text

Pipes A way of extending the change detection • Format data • Perform smarter change detection

Slide 168

Slide 168 text

Dynamic Change Detector _pipeCheck(proto: ProtoRecord, throwOnChange: boolean) { // ... var currValue = pipe.transform(context); if (!isSame(prevValue, currValue)) { // ... this._setChanged(proto, true); return change; } else { // ... this._setChanged(proto, true); return null; } } else { // ... this._setChanged(proto, false); return null; } }

Slide 169

Slide 169 text

Pipes A few examples: • IterableChanges • PromisePipe • UpperCasePipe • ObservablePipe • JsonPipe

Slide 170

Slide 170 text

PromisePipe
{{ description | async }}

Slide 171

Slide 171 text

UpperCasePipe
{{ "foo" | UpperCase }}

Slide 172

Slide 172 text

IterableDiff (foreach.js) set ngForOf(value: any) { this._ngForOf = value; this._pipe = this.pipes .get("iterableDiff",…); }

Slide 173

Slide 173 text

Dependency Injection

Slide 174

Slide 174 text

Router

Slide 175

Slide 175 text

Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html', directives: [RouterOutlet, RouterLink] }) class App {} bootstrap(App, [routerInjectables]);

Slide 176

Slide 176 text

Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html', directives: [RouterOutlet, RouterLink] }) class App {} bootstrap(App, [routerInjectables]);

Slide 177

Slide 177 text

Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html', directives: [RouterOutlet, RouterLink] }) class App {} bootstrap(App, [routerInjectables]);

Slide 178

Slide 178 text

Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html', directives: [RouterOutlet, RouterLink] }) class App {} bootstrap(App, [routerInjectables]);

Slide 179

Slide 179 text

Router import {RouteConfig, RouterOutlet, RouterLink, routerInjectables} from 'angular2/router'; @Component({ selector: 'app' }) @RouteConfig([ { path: '/', component: Home, as: 'home' }, { path: '/about', component: About, as: 'about' } ]) @View({ templateUrl: './app.html', directives: [RouterOutlet, RouterLink] }) class App {} bootstrap(App, [routerInjectables]);

Slide 180

Slide 180 text

Router Directives Home About

Slide 181

Slide 181 text

Router Directives Home About

Slide 182

Slide 182 text

Router Directives Home About

Slide 183

Slide 183 text

References • Angular 2 Official Website • Change And Its Detection In JavaScript Frameworks • Angular2 - First Impressions • ng-conf 2015 Keynote 2 - Misko Hevery and Rado Kirov • angular2-seed • angular2-education

Slide 184

Slide 184 text

Thank you! github.com/mgechev twitter.com/mgechev blog.mgechev.com