1 of 35

Angular 2 Core

Igor Minar

Tobias Bosch

ng-europe 2014

video | follow-up post

2 of 35

3 of 35

4 of 35

5 of 35

AngularJS 1.3

<div ng-controller="SantaTodoController">

<input type="text" ng-model="newTodoTitle">

<button ng-click="addTodo()">+</button>

<tab-container>

<tab-pane title="Tobias">

<div ng-repeat="todo in todosOf('tobias')">

<input type="checkbox"

ng-model="todo.done">

{{todo.title}}

<button ng-click="deleteTodo(todo)">

X

</button>

</div>

</tab-pane>

...

6 of 35

AngularJS 2.0

<div>� <input type="text" [value]="newTodoTitle">� <button (click)="addTodo()">+</buton>�� <tab-container>� <tab-pane title="Good kids">� <div [ng-repeat|todo]="todosOf('good')">� <input type="checkbox"

[checked]="todo.done">� {{todo.title}}� <button (click)="deleteTodo(todo)">

X

</button>� </div>

</tab-pane>�...

7 of 35

AngularJS 2.0

<div>� <input type="text" [value]="newTodoTitle">� <button (click)="addTodo()">+</buton>�� <tab-container>� <tab-pane title="Good kids">� <div [ng-repeat|todo]="todosOf('good')">� <input type="checkbox"

[checked]="todo.done">� {{todo.title}}� <button (click)="deleteTodo(todo)">

X

</button>� </div>

</tab-pane>�...

[value]="newTodoTitle"

(click)="addTodo()"

[ng-repeat|todo]="todosOf('good')"

[checked]="todo.done"

{{todo.title}}

(click)="deleteTodo(todo)"

8 of 35

AngularJS 2.0

<div>� <input type="text" [value]="newTodoTitle">� <button (click)="addTodo()">+</buton>�� <tab-container>� <tab-pane title="Good kids">� <div [ng-repeat|todo]="todosOf('good')">� <input type="checkbox"

[checked]="todo.done">� {{todo.title}}� <button (click)="deleteTodo(todo)">

X

</button>� </div>

</tab-pane>�...

9 of 35

AngularJS 2.0

<div>� <input type="text" [value]="newTodoTitle">� <button (click)="addTodo()">+</buton>�� <tab-container>� <tab-pane title="Good kids">� <div [ng-repeat|todo]="todosOf('good')">� <input type="checkbox"

[checked]="todo.done">� {{todo.title}}� <button (click)="deleteTodo(todo)">

X

</button>� </div>

</tab-pane>�...

[checked]="todo.done"

10 of 35

Databinding

<elm attr="...">

elm.property=...

11 of 35

12 of 35

13 of 35

controllers

2009-2014

14 of 35

Directive Definition Object ("DDO")

myModule.directive('directiveName', function factory(injectables) {� return {� priority: 0,� template: '<div></div>', // or // function(tElement, tAttrs) { ... },� // or� // templateUrl: 'directive.html', // or // function(tElement, tAttrs) { ... },� transclude: false,� restrict: 'A',� templateNamespace: 'html',� scope: false,� controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... },� controllerAs: 'stringAlias',� require: 'siblingDirectiveName', // or // ['^parentDirectiveName', '?optionalDirectiveName', '?^optionalParent'],� compile: function compile(tElement, tAttrs, transclude) {� return {� pre: function preLink(scope, iElement, iAttrs, controller) { ... },� post: function postLink(scope, iElement, iAttrs, controller) { ... }� }� // or� // return function postLink( ... ) { ... }� },� // or� // link: {� // pre: function preLink(scope, iElement, iAttrs, controller) { ... },� // post: function postLink(scope, iElement, iAttrs, controller) { ... }� // }� // or� // link: function postLink( ... ) { ... }� };�});

15 of 35

DDO

2009-2014

16 of 35

SantaTodoApp component

@ComponentDirective

class SantaTodoApp {

constructor() {

this.newTodoTitle = '';

}

addTodo: function() { ... }

removeTodo: function(todo) { ... }

todosOf: function(filter) { ... }

}

17 of 35

SantaTodoApp component

@ComponentDirective({ ... })

class SantaTodoApp { ... }

@TemplateDirective({ ... })

class NgRepeat { ... }

@DecoratorDirective({ ... })

class NgClass { ... }

18 of 35

SantaTodoApp component

@ComponentDirective

class SantaTodoApp {

constructor() {

this.newTodoTitle = '';

}

addTodo: function() { ... }

removeTodo: function(todo) { ... }

todosOf: function(filter) { ... }

}

19 of 35

$scope

2009-2014

20 of 35

ng-repeat and $parent

Selected user: {{selectedUser.name}}

<div ng-repeat="user in users"

ng-click="selectedUser = user">

</div>

21 of 35

ng-repeat and $parent

Selected user: {{selectedUser.name}}

<div ng-repeat="user in users"

ng-click="$parent.selectedUser = user">

</div>

22 of 35

23 of 35

Registering directives

@ComponentDirective({

directives: [TabContainer, TabPane, NgRepeat]

})

class SantaTodoApp {

constructor() {

this.newTodoTitle = '';

}

addTodo: function() { ... }

removeTodo: function(todo) { ... }

todosOf: function(filter) { ... }

}

24 of 35

Dependency Injection

25 of 35

Defining services

class TodoStore {

constructor(win:Window) {

this.win = win;

}

add(todo) {

// access this.win.localStorage ...

}

remove(todo) { ... }

todosOf(filter) { ... }

}

26 of 35

angular�.module

2009-2014

27 of 35

Using services

import {TodoStore} from './store';

@ComponentDirective({

directives: [TabContainer, TabPane, NgRepeat]

})

class SantaTodoApp {

constructor(store:TodoStore) {

...

}

...

}

28 of 35

Directives and DI

<tab-container>

<tab-pane title="Tobias">

New Macbook Pro

Tesla Model X

...

</tab-pane>

<tab-pane title="Good kids">

...

</tab-pane>

<tab-pane title="Bad kids">

...

</tab-pane>

</tab-container>

29 of 35

Directives and DI

class TabContainer {

constructor(tabs:Query<TabPane>) { ... }

...

}

class TabPane {

constructor(

tabContainer:TabContainer,

element:HTMLElement

) { ... }

...

}

30 of 35

Performance

Measure

Analyze

Improve

31 of 35

Measure

32 of 35

Macro optimizations

jqLite

2009-2014

<div>� <input type="text" [value]="newTodoTitle">� <button (click)="addTodo()">+</buton>�� <tab-container>� <tab-pane title="Good kids">� <div [ng-repeat|todo]="todosOf('good')">� <input type="checkbox"

[checked]="todo.don� {{todo.title}}� <button (click)="deleteTo

X

</button>� </div>

</tab-pane>�...

33 of 35

Analyze

34 of 35

Analyze - Web Tracing Framework

35 of 35

Summary

controllers

DDO

$scope

angular.module

jqLite

generic binding syntax

benchpress

WTF instrumentation

DI query mechanism

ng-europe 2014

video | follow-up post