More Related Content Similar to Angular 2 Architecture (Bucharest 26/10/2016) (20) More from Eyal Vardi (15) Angular 2 Architecture (Bucharest 26/10/2016)6. <h1> Hi {{name}} </h1>
<div [property]="value" ></div>
<div (click)="setActive(todo)" ></div>
<input type="text" [(ngModel)]="todo.done">
One-way (Input):
One-way (Output):
Two-way:
8. <div *ngFor="let todo of todos" >
<input type="checkbox" [(ngModel)]="todo.done" >
<span (click)="setActive(todo)"
[class.done]="todo.done" >
{{todo.text}}
</span>
</div>
9. @Component({
selector: 'todo-list',
template: `
<div *ngFor="let todo of todos">
<input type="checkbox" [(ngModel)]="todo.done">
<span (click)="setActive(todo)"
[class.done]="todo.done">
{{todo.text}}
</span>
</div>
`})
export class TodoList {
@Output() selectedChange = new EventEmitter()
@Input('source') todos: Todo[] = [];
constructor(private db:Db, private proxy:Proxy){}
}
10. @Component({
selector: 'todo-list',
template: `
<div *ngFor="let todo of todos">
<input type="checkbox" [(ngModel)]="todo.done">
<span (click)="setActive(todo)"
[class.done]="todo.done">
{{todo.text}}
</span>
</div>
`})
export class TodoList {
@Output() selectedChange = new EventEmitter()
@Input('source') todos: Todo[] = [];
constructor(private db:Db, private proxy:Proxy){}
}
<todo-list [source]="todos"
(selected-change)="update($event)" />
19. FormsModule
exports
(NgModel…)
CommonModule
exports
( NgIf , … )
My Components,
Directives & Pipes
Template Context@NgModule({
declarations:[...],
imports :[...],
exports :[...],
bootstrap :[...]
})
export class XxxModule{}
@NgModule({
declarations:[...],
imports :[...],
exports :[...],
})
export class XxxModule{}
@NgModule({
declarations:[...],
imports :[...]
})
export class XxxModule{}
@NgModule({
declarations:[...]
})
export class XxxModule{}
@NgModule({
})
export class XxxModule{}
20. Application Injector
XxxModule
providers
RouterModule
providers
Lazy Loading
Boundary
Module Injector
MathModule
providers
UsersModule
providers
@NgModule({
declarations:[...],
imports :[...],
exports :[...],
bootstrap :[...],
providers :[...]
})
export class XxxModule{}
@NgModule({
declarations:[...],
imports :[...],
exports :[...],
bootstrap :[...],
providers :[...]
})
export class XxxModule{}
@NgModule({
declarations:[...],
imports :[...],
exports :[...],
bootstrap :[...],
providers :[...]
})
export class XxxModule{}
Platform
Injector
24. @NgModule({
imports: [...],
declarations: [...],
exports: [...],
providers: [...]
})
export class CoreModule {
constructor (@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule is already loaded.');
}
}
static forRoot(config: UserServiceConfig): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [
{provide: UserServiceConfig, useValue: config }
]
};
}
}
Prevent
reimport
Configure
core services
31. export class CounterComponent {
value:number = 0;
constructor(private zone:NgZone,
private cd :ChangeDetectorRef){
setInterval( ()=>{ this.value++; } , 50 );
}
}
Ticks
Never use
setInterval method
setInterval( ()=>{ this.value++; } , 50 );
Update
32. export class CounterComponent {
value:number = 0;
constructor(private zone:NgZone,
private cd :ChangeDetectorRef){
run();
}
run(){
this.value++;
setTimeout( ()=> { this.run() } , 50 );
}
}
Create method
every time
Ticks Update
setTimeout( ()=> { this.run() } , 50 );
33. export class CounterComponent {
value : number = 0;
runFnBind : any;
constructor(private zone:NgZone,
private cd :ChangeDetectorRef){
this.runFnBind = this.run.bind(this);
run();
}
run(){
this.value++;
setTimeout( this.runFnBind , 50 );
}
}
Ticks Update
this.runFnBind = this.run.bind(this);
setTimeout( this.runFnBind , 50 );
34. export class CounterComponent {
value : number = 0;
runFnBind : any;
constructor(private zone:NgZone,
private cd :ChangeDetectorRef){
this.runFnBind = this.run.bind(this);
zone.runOutsideAngular( this.runFnBind );
}
run(){
this.value++;
setTimeout( this.runFnBind , 50 );
}
}
Ticks Update
zone.runOutsideAngular( this.runFnBind );
35. export class CounterComponent {
value:number = 0;
constructor(private zone:NgZone,
private cd :ChangeDetectorRef){
zone.runOutsideAngular( this.run.bind(this) );
}
run(){
this.value++;
this.cd.detectChanges();
setTimeout( this.run.bind(this) , 50 );
}
}
Ticks Update
this.cd.detectChanges();
40. export class MonitorComponent {
...
constructor(private cd :ChangeDetectorRef){
cd.detach();
}
...
set serverLoadValue(val){
let isthreshold = this.checkThreshold(val);
this._serverLoadValue = val;
if(isthreshold){
this.cd.detectChanges();
}
}
}
cd.detach();
this.cd.detectChanges();
Editor's Notes Angular 2 will scan the entire tree component and calculate each expression every 50ms.