3

I found in some code of my colleagues detectChanges() and markForCheck(). They are using the default change detection strategy and no NgZones. As far as I know detectChanges and markForCheck is only necessary if you use OnPush strategy or NgZones.

As far as I know these are the use cases for the methods: detectChanges => if you are using runOutsideAngular for example () or somethink like getElementById() and manipulate and element (which should be avoided in angular)

markForCheck => if an object was mutating + OnPush strategy

So is Angulars detectChanges() and markForCheck() relevant for the default change detection strategy?

1 Answer 1

2

Yesn't.

If you're not using ChangeDetectionStrategy.OnPush or ChangeDetectorRef, then both of the methods won't do anything.

You can however, detach the change detector, even with default change detection. in that case you can use detectChanges() to manually check the view.

Read more about change detection here.

Sign up to request clarification or add additional context in comments.

3 Comments

they are also not detaching the change detector. They just use the default change detection strategy together with MarkForCheck or detectChanges. But I don't understand the purpose. They also don't use NgZones or any JQuery. Maybe the markForCheck() calls are necessary
@IsaacTack If there are no additional shenanagans, most likely they do nothing. I'd suggest simply asking your colleagues why they used it in the first place.
I think they also not so sure about that :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.