Could you please help me differentiate those two things.
According to my understanding, if you only work with observable, you can use detectChanges(). So you can change a component property directly or spy on a service call and return an observable, then you call detectChanges(), the changes will be available on html elements.
But for [(ngModel)] on input fields you need to call tick() for the changes to be render on html element.
So I'd it would be great if I know what they do and when to use what.
Thanks in advance.
detectChanges
The method
detectChanges
is available on the ViewRef.ViewRef
is an underlying representation of a component. When writing tests instead ofViewRef
another abstraction is introduced which isfixture
:It wraps the component similar to
ViewRef
.detectChanges
method runs change detection for the underlying component and performs the following operations:and many others.
To learn more you can read Everything you need to know about change detection in Angular. So in order to validate changes in the DOM or validate input bindings you need to run
detectChanges
.tick
Angular docs describe it pretty well:
With
ngModel
you need to call it because the control that is created insidengModel
is registered asynchonously. Here is the quote from the article by Victor Savkin on forms: