angular 2 architecture

45
Angular 2.x Architecture 2 Eyal Vard Site: http://ng-course.o Blog: eyalVardi.wordpress.

Upload: eyal-vardi

Post on 09-Jan-2017

1.740 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Angular 2 Architecture

Angular 2.x Architecture

2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

Page 2: Angular 2 Architecture

Agenda What is Angular? Bootstrap Injector Compile Resolvers (link) Tick ($apply)

Page 3: Angular 2 Architecture

What is Angular 2?

Page 4: Angular 2 Architecture

Compo

nent

s

Page 5: Angular 2 Architecture

Building Blocks of an Angular 2

System

shim

ZoneReflectRx

Libraries

Application

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

core & common

Angular Frameworks

Router Http Compiler

Platform

Page 6: Angular 2 Architecture

@Component({ selector: 'todo-list', styles: [` .done{ text-decoration: line-through; color: grey; }`], template: ` <ul class="list-unstyled"> <li *ngFor="#todo of todos"> <input type="checkbox" [(ngModel)]="todo.done"> <span [class.done]="todo.done">{{todo.text}}</span> </li> </ul>`})

export class TodoList {

@Output() selectedChange = new EventEmitter() @Input('source') todos: Todo[] = [];

constructor(private db:Db, private proxy:Proxy){}}

{{exp}}

[property]="exp"

(event) ="exp"

*ngIf ="exp"

*ngFor ="exp"

Page 7: Angular 2 Architecture

DOM Tree<div class="container">

<div class="starter-template">

<a href="http://ng-course.org" target="_blank">

<img src="images/ng-course.png" width="500">

</a>

<br>

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

</div>

</div>

Page 8: Angular 2 Architecture

Bootstrap

Page 9: Angular 2 Architecture

Angular 2.0 Bootstrap<html> <head> . . . <script src="shim.js"></script> <script src="zone.js"></script> <script src="Reflect.js"></script> <script src="system.js"></script>

<script> System.import('app/main'); </script> </head> <body>

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

Async

1

3

2

Page 10: Angular 2 Architecture

Angular 2.0 Bootstrap

import {bootstrap} from '@angular/platform-browser-dynamic';

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 11: Angular 2 Architecture

PlatformRef

PlatformRef Each page has exactly one platform.

ApplicationRef

I

I Injector ZZone CChange DetectionRRenderer

IZCR

IC

IC

IC

IC ICIC

Page 12: Angular 2 Architecture

Bootstrap Code // Create Platform Injector platform(BROWSER_PLATFORM_PROVIDERS) // Create Application Injector .application([

BROWSER_APP_PROVIDERSBROWSER_APP_COMPILER_PROVIDERS, appProviders

]); // Create Injector => Compile => tick => Create Classes .bootstrap( MyApp );

Page 13: Angular 2 Architecture

Injector

Page 14: Angular 2 Architecture

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 15: Angular 2 Architecture

Injectors TreePlatform

BROWSER_PLATFORM_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 16: Angular 2 Architecture

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 17: Angular 2 Architecture

Compiletime vs. Runtimeimport {Component} from "@angular/core";import {Type1,Type2,Type3} from "eyalVardi";import {Pipe1,Directive1} from "eyalVardi";import {Component2} from "eyalVardi";import {Service1} from "eyalVardi";

@Component({ providers :[Type1,Type2], viewProviders:[Type3], pipes :[Pipe1], directives :[Directive1,Component2]})export class MyComponent { constructor( element : ElementRef, type1 : Type1 ){}}

Can solve by theimport?

Must solve in runtime

by the Injector

Page 18: Angular 2 Architecture

The Provider Class

[ServiceA]

[{provide:ServiceA, useClass:ServiceA}]

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

==

==

 token  "recipe" for creating

Page 19: Angular 2 Architecture

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

useClass? : Type,

useValue? : any,

useExisting?: any,

useFactory? : Function,

deps? : Object[],

multi? : boolean})

Page 20: Angular 2 Architecture

Component Injectors

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

Component Injector

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

Page 21: Angular 2 Architecture

App Template

Component3 Template

Tree ComponentsApp

Component1

Component2

Directive1

Component3

loading

Template Content

<app>Loading</app>

<div> <component1 directive1="{{name}}"> <component2> This is content. </component2> </component1></div>

<div> <h4>{{name}}</h4> <ng-content /></div>

This is content

<div> <h4>{{name}}</h4> <ng-content /></div>

Page 22: Angular 2 Architecture

Component Injector

Component Directive

TemplateContent

*

* *

viewProviders

directivespipes

Providers@ContentChild@ContentChildren

@ViewChild @ViewChildren

DOM ElementProviders

Page 23: Angular 2 Architecture

Providers vs. viewProviders

Page 24: Angular 2 Architecture

Global Components & Pipes

bootstrap(AppComponent, [

{provide: PLATFORM_DIRECTIVES, useValue: [myComponent],

multi:true},

{provide: PLATFORM_PIPES , useValue: [myPipe] ,

multi:true}

]);Available in every component of the application

Page 25: Angular 2 Architecture

@Global()

Page 26: Angular 2 Architecture

Tabs (@ContentChildren)

Page 27: Angular 2 Architecture

Component Interaction import @Input ExportAs Constructor @ContentChild & @ContentChildren @ViewChild & @ ViewChildren

Page 28: Angular 2 Architecture

Constructor Injection@Component({...})export class MyComponent { constructor( element : ElementRef , changeDetector: ChangeDetectorRef , viewContainer : ViewContainerRef , template : TemplateRef , render : Renderer ){...}}

Page 29: Angular 2 Architecture

Compile

Page 30: Angular 2 Architecture

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 31: Angular 2 Architecture

Runtime Metadata Resolver DirectiveResolver

templateUrl?

template?

directives?

pipes?

encapsulation?

styles?

styleUrls?

selector?

inputs?

outputs?

host?

providers?

exportAs?

queries?

ViewResolver

UrlResolver

Page 32: Angular 2 Architecture

Component Multi Views

Page 33: Angular 2 Architecture

Link

Page 34: Angular 2 Architecture

Link Function vs. Class Constructor ElementRef

Renderer

TemplateRef

ViewContainerRef

DynamicComponentLoader

ComponentResolver

Page 35: Angular 2 Architecture

Dynamic Component

Page 36: Angular 2 Architecture

Tick

Page 37: Angular 2 Architecture

Angular 2.0 Tick Cycle

IC

IC

IC

IC ICIC

NgZoneCommunication,Timers, UI Events

Tim

e

Screen Update

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

Page 38: Angular 2 Architecture

Infinity Loop

Page 39: Angular 2 Architecture

Change Detector

F x Q = Time

IC

IC

IC

IC ICIC

Page 40: Angular 2 Architecture
Page 41: Angular 2 Architecture

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 42: Angular 2 Architecture

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

Page 43: Angular 2 Architecture

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

1

2

3

4

5

6

7

Page 44: Angular 2 Architecture

Change Detector

Page 45: Angular 2 Architecture

eyalvardi.wordpress.com

Thanks2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com