EXCEPTION: Attempt to use a destroyed view

Sometimes, especially when manually works with change detection, may occur exception from the title of this post.

According to my experience, this exception always appears from the detectChanges() method.

I haven’t yet dig down source code of this to clearly understand the root of this exception. But if take a think about it:

I call this method when it normally no need to call.
when all changes were done and now the active destroying phase of the component.

In one case, if for the component used OnPush change detection strategy using markForCheck() instead detectChanges() will resolve the exception.

But it not works when no need to use onPush strategy.

So the next case — implementing ngOnDestroy() method from the OnDestroy interface. And inside this method detach from ChangeDetectorRef.

constructor(private ref:ChangeDetectorRef){...}

ngOnDestroy() {

If you don’t use calling detectChanges() inside timeouts — that’s enough.

But if yes — exception still occurs. In this third case, I check if my ChangeDetectorRef instance has ‘destroyed’ flag up.

if (!(this.ref as ViewRef).destroyed) {

or even simple


Leave a Reply

Your email address will not be published. Required fields are marked *