Every angular 2 app is a reactive component tree. This means that every node (component) is capable of check for its own changes and decide wether to update or not.
This gives the developer an unprecedented level of control over change detection.
Zones can perform an operation - such as starting or stopping a timer, or saving a stack trace - each time that code enters or exits a zone.
function main() {
foo();
setTimeout(doSomething, 2000);
bar();
baz();
}
var myZoneSpec = {
beforeTask: function () {
console.log('Before task');
},
afterTask: function () {
console.log('After task');
}
};
var myZone = zone.fork(myZoneSpec);
myZone.run(main);
// Logs:
// Before task
// After task
// Before task
// Async task
// After taskAngular creates change detector classes at runtime for each component.
@Component({
template: `
<h2>{{vData.name}}</h2>
<span>{{vData.email}}</span>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class VCardCmp {
@Input() vData;
}@Component({
template: '{{counter}}',
changeDetection: ChangeDetectionStrategy.OnPush
})
class CartBadgeCmp {
@Input() addItemStream:Observable<any>;
counter = 0;
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.addItemStream.subscribe(() => {
this.counter++; // application state changed
this.cd.markForCheck(); // marks path
})
}
}Ideally, a component should be able to process inputs and produce an output in isolation.
It should have well defined boundaries.
Component
State
Actions
UI
BL
Rx, Store, Router, Hooks