I am reading this article which has a section on when to use markForChange()
.
In his example he has the following component:
@Component({
selector: 'cart-badge',
template: '{{counter}}',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CartBadgeComponent {
@Input() addItemStream;
counter = 0;
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.addItemStream.subscribe(() => {
this.counter++;
this.cd.markForCheck();
});
}
}
And he uses the markForCheck()
method in order to update the view. The thing that I don't understand is that why we need to use markForCheck()
here when a call to detectChanges()
is also updating the view?
I read the answer on StackOverflow for the question - What's the difference between markForCheck() and detectChanges()?
But it does not meet the example above. So, why don't call detectChanges()
instead markForCheck()
?