angular 2 architecture

Post on 09-Jan-2017

1.740 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Angular 2.x Architecture

2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

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

What is Angular 2?

Compo

nent

s

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

@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"

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>

Bootstrap

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

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

PlatformRef

PlatformRef Each page has exactly one platform.

ApplicationRef

I

I Injector ZZone CChange DetectionRRenderer

IZCR

IC

IC

IC

IC ICIC

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 );

Injector

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){ //... }}

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

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

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

The Provider Class

[ServiceA]

[{provide:ServiceA, useClass:ServiceA}]

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

==

==

 token  "recipe" for creating

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

useClass? : Type,

useValue? : any,

useExisting?: any,

useFactory? : Function,

deps? : Object[],

multi? : boolean})

Component Injectors

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

Component Injector

<component my-

directive>

<sub-comp/>

<sub-comp/>

</component>

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>

Component Injector

Component Directive

TemplateContent

*

* *

viewProviders

directivespipes

Providers@ContentChild@ContentChildren

@ViewChild @ViewChildren

DOM ElementProviders

Providers vs. viewProviders

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

@Global()

Tabs (@ContentChildren)

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

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

Compile

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

Runtime Metadata Resolver DirectiveResolver

templateUrl?

template?

directives?

pipes?

encapsulation?

styles?

styleUrls?

selector?

inputs?

outputs?

host?

providers?

exportAs?

queries?

ViewResolver

UrlResolver

Component Multi Views

Link

Link Function vs. Class Constructor ElementRef

Renderer

TemplateRef

ViewContainerRef

DynamicComponentLoader

ComponentResolver

Dynamic Component

Tick

Angular 2.0 Tick Cycle

IC

IC

IC

IC ICIC

NgZoneCommunication,Timers, UI Events

Tim

e

Screen Update

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

Infinity Loop

Change Detector

F x Q = Time

IC

IC

IC

IC ICIC

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

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

Hooks Order

Component

TemplateContent

* *

OnChangesOnInit

AfterContentInitAfterContentChecked

AfterViewInitAfterViewChecked

DoCheck

1

2

3

4

5

6

7

Change Detector

eyalvardi.wordpress.com

Thanks2

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

top related