Change detection
Menu
Header
Footer
Feed
Breaking news!
Meows news!
App
Menu
Header
Feed
Footer
Post
Post
posts: Post[] = [
{
id: 1,
title: 'Breaking news!',
text: 'Voluptate labore veniam...',
likes: 2,
isLiked: false
},
{
id: 2,
title: 'Meows news!',
text: 'Laborum minim sit incididun...',
likes: 42,
isLiked: false
}
];
Menu
Header
Footer
Feed
Breaking news!
Meows news!
posts: Post[] = [
{
id: 1,
title: 'Breaking news!',
text: 'Voluptate labore veniam...',
likes: 2,
isLiked: false
},
{
id: 2,
title: 'Meows news!',
text: 'Laborum minim sit incididun...',
likes: 43,
isLiked: true
}
];
2 ♡
42 ♡
43 ♡
Model
DOM
Component
Application State Update
Is done by YOUR code after some browser event (user interaction)
View State Propogation
Is done by Change Detector after your code is finished
<parent>
<child>
...
</child>
</parent>
C - time to check one binding
N - amount of bindings
C - time to check one binding
N - amount of bindings
Compiles at runtime
Compiles at build time
$ ng serve
$ ng build
$ ng serve --aot
$ ng build --prod
Essential to be enabled in production mode
C - time to check one binding
N - amount of bindings
@Component({
// ...,
changeDetection: ChangeDetectionStrategy.OnPush,
// ...
})
export class CardItemComponent {
// ...
}
+ Immutable objects
Manually controls change detection flow
@Component({
// ...,
})
export class Myomponent {
constructor(private ref: ChangeDetectorRef) {
// ...
}
}
Methods
Async action
Change detection
Zone.js
constructor (private ngZone: NgZone) {}
public processOutside() {
this.ngZone.runOutsideAngular( () => {
// do some stuff
this.goBack();
});
}
public goBack(someFunction) {
this.ngZone.run(someFunction);
}