angular 1.x vs. angular 2.x

38
Angular 1.x vs. Angular 2.0 2 Eyal Vard Site: http://ng-course.o Blog: eyalVardi.wordpress.

Upload: eyal-vardi

Post on 20-Jan-2017

4.712 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Angular 1.x vs. Angular 2.x

Angular 1.x vs.

Angular 2.0

2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

Page 2: Angular 1.x vs. Angular 2.x

Bootstrap

Page 3: Angular 1.x vs. Angular 2.x

Angular 1.x Bootstrapvar injector = createInjector(modules);

injector.invoke([

'$rootScope','$rootElement','$compile','$injector','$animate',

function(scope, element, compile, injector, animate) {

scope.$apply(function() {

element.data('$injector', injector);

compile(element)(scope);

});

}]

);

Create injector(config & run)

1

Compile the root element and return link function.

2 Execute the link function with the root scope.

3

Apply, update

the page

4

Page 4: Angular 1.x vs. Angular 2.x

Angular 2.0 Bootstrap

<html> <head> . . . <script src="angular2.js"></script> <script src="router.js"></script> <script src="http.js"></script> <script> System.import('app/main'); </script> </head> <body>

<my-app>Loading...</my-app> </body></html>

Async

Page 5: Angular 1.x vs. Angular 2.x

Angular 2.0 Bootstrap

import {bootstrap} from 'angular2/platform/browser';

import {AppComponent} from './app.component';

bootstrap(AppComponent);

Load Tree !!!1

Create Platform

2

Create Application

3

Compile AppComponent

4

Tick&

Link (Create Classes)

5

Page 6: Angular 1.x vs. Angular 2.x

PlatformRef

PlatformRef Each page has exactly one platform.

ApplicationRef

I

I Injector ZZone CChange DetectionRRenderer

IZCR

IC

IC

IC

IC ICIC

Page 7: Angular 1.x vs. Angular 2.x

Bootstrap Code // Create Platform Injector platform( BROWSER_PROVIDERS ) // Create Application Injector .application([ BROWSER_APP_PROVIDERS, appProviders ]); // Create Injector => Compile => tick => Create Classes .bootstrap( MyApp );

Page 8: Angular 1.x vs. Angular 2.x

Injector

Page 9: Angular 1.x vs. Angular 2.x

ngXXX

Module Summaryangular.module('myApp', ['ngXXX', 'ngYYY']);

InvokeQueue

ngYYY

InvokeQueue

myApp

InvokeQueue

Configblocks

Configblocks

Configblocks

Runblocks

Runblocks

Runblocks

$injector

Instance Cache

ProviderCache

Page 10: Angular 1.x vs. Angular 2.x

A

Child Injector

Parent InjectorA,B,C

Child Injector

A,B

Child Injector

A

var p = Injector.resolveAndCreate([A,B,C])

var c1 = p.resolveAndCreateChild([A,B])

var c2 = c1.resolveAndCreateChild([A])

c2.get(A) =>

B C@Injectable()class A{ constructor(b:B,c:C){ //... }}

Page 11: Angular 1.x vs. Angular 2.x

Injectors TreePlatform

BROWSER_PROVIDERS

ApplicationBROWSER_APP_PROVIDERS & CustomProviders

IC

IC

IC

IC ICIC

Component Metadata

providers viewProviders directives pipes

PLATFORM_PIPES PLATFORM_DIRECTIV

ES FORM_PROVIDERS DOCUMENT DomRootRenderer . . .

PLATFORM_INITIALIZER

Reflector Console

Page 12: Angular 1.x vs. Angular 2.x

Component Injectors

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

Component Injector

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

Page 13: Angular 1.x vs. Angular 2.x

Component Injector

Component Directive

TemplateContent

*

* *

viewProviders

directivespipes

Providers@ContentChild@ContentChildren

@ViewChild@ViewChildre

n

DOM Element

Page 14: Angular 1.x vs. Angular 2.x

@Global()

Page 15: Angular 1.x vs. Angular 2.x

The Provider Class

[ServiceA]

[provide(ServiceA, {useClass:ServiceA})]

[new Provider(ServiceA, {useClass:ServiceA})]

==

==

 token  "recipe" for creating

Page 16: Angular 1.x vs. Angular 2.x

Provider Classconstructor(token: any, { useClass, useValue, useExisting, useFactory, deps, multi }: {

useClass? : Type,

useValue? : any,

useExisting?: any,

useFactory? : Function,

deps? : Object[],

multi? : boolean})

Page 17: Angular 1.x vs. Angular 2.x

ComponentMetadata

Component Metadata Names:

selector? : string exportAs? : string

Binding: inputs? : string[] outputs? : string[] host? : {[key: string]: string} changeDetection?: ChangeDetectionStrategy

View: templateUrl? : string template? : string styleUrls? : string[] styles? : string[] encapsulation?:

ViewEncapsulation

Injector: providers? : any[] viewProviders? : any[] directives? : Array<Type | any[]> pipes? : Array<Type | any[]> queries? : {[key: string]: any} Directive

Metadata

Page 18: Angular 1.x vs. Angular 2.x

Compile

Page 19: Angular 1.x vs. Angular 2.x

Directive Definition Object (DDO)myModule.directive('directiveName', function factory(injectables) { var DDO = {

priority: 0,

replace: false,

transclude: false,

restrict: 'EA',

terminal: false,

template: '<div></div>',

templateUrl:'directive.html',

compile: function(tElement, tAttrs, transclude) { ... }};

return DDO;});

This properties affect the

$compile service.Beha

vior

s

Page 20: Angular 1.x vs. Angular 2.x

ComponentMetadata

Component Metadata Names:

selector? : string exportAs? : string

Binding: inputs? : string[] outputs? : string[] host? : {[key: string]: string} changeDetection?: ChangeDetectionStrategy

View: templateUrl? : string template? : string styleUrls? : string[] styles? : string[] encapsulation?:

ViewEncapsulation

Injector: providers? : any[] viewProviders? : any[] directives? : Array<Type | any[]> pipes? : Array<Type | any[]> queries? : {[key: string]: any} Directive

Metadata

Page 21: Angular 1.x vs. Angular 2.x

Runtime Metadata Resolver DirectiveResolver

templateUrl?

template?

directives?

pipes?

encapsulation?

styles?

styleUrls?

selector?

inputs?

outputs?

host?

providers?

exportAs?

queries?

ViewResolver

UrlResolver

Page 22: Angular 1.x vs. Angular 2.x

Component Multi Views

Page 23: Angular 1.x vs. Angular 2.x

Link

Page 24: Angular 1.x vs. Angular 2.x

Directive Definition Object (DDO)myModule.directive('directiveName', function factory(injectables) { var DDO = { priority: 0, terminal: false, template: '<div></div>', templateUrl:'directive.html', replace: false, transclude: false, restrict: 'A', compile: function compile(tElement, tAttrs, transclude) {...},

scope: false, require: '^?ngModel' controller: function($scope, $element, $attrs, $transclude, Injectables) { ... }, link: function postLink(scope, iElement, iAttrs, controller) { ... } };

return DDO;});

Link

Com

pile

Page 25: Angular 1.x vs. Angular 2.x

Link Function vs. Class Constructor ElementRef

TemplateRef

ViewContainerRef

Renderer

DynamicComponentLoader

Page 26: Angular 1.x vs. Angular 2.x

Dynamic Component

Page 27: Angular 1.x vs. Angular 2.x

Apply

Page 28: Angular 1.x vs. Angular 2.x

Scope's $digest() Flow

$evelAsyncQueue

TraverseScopeLoop

$$postDigest

Screen Update

Tim

e

Can enter an infinite

loop

Need explicitly

call

Page 29: Angular 1.x vs. Angular 2.x

Angular 2.0 Tick Cycle

IC

IC

IC

IC ICIC

NgZoneCommunication,Timers, UI Events

Tim

e

Screen Update

{{interpolation}} [property] = "exp" (event) = "exp"

Page 30: Angular 1.x vs. Angular 2.x

Infinity Loop

Page 31: Angular 1.x vs. Angular 2.x

Change Detector

F x Q = Time

IC

IC

IC

IC ICIC

Page 32: Angular 1.x vs. Angular 2.x

Lifecycle Hooks Angular calls lifecycle hook methods on

directives and components as it creates, changes, and destroys them.

Creates: OnInit AfterContentInit AfterViewInit

Changes: DoCheck OnChanges AfterContentChecked AfterViewChecked

Destroys: OnDestroy

Page 33: Angular 1.x vs. Angular 2.x

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

Page 34: Angular 1.x vs. Angular 2.x

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

1

2

3

4

5

6

7

Page 35: Angular 1.x vs. Angular 2.x

ChangeDetector

Page 36: Angular 1.x vs. Angular 2.x

Angulal2.js

Router.js Http.js Rx.js

ServicesComponents Directives

Page 37: Angular 1.x vs. Angular 2.x

eyalvardi.wordpress.com

Thanks2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

Page 38: Angular 1.x vs. Angular 2.x

Component Structure<todo-list [source]="todos" (selected-change)="update($event)" />

Style

Injector

Class

Template

InputOutput