Skip to content

Commit c04f941

Browse files
fix(cdk/portal): prevent calling ApplicationRef.detachView on destroyed instance (#24775)
This commit adds a check that ensures that the `ApplicationRef.detachView` is only called when there are some registered views in the `ApplicationRef` instance (thus the instance is not destroyed).
1 parent 4193037 commit c04f941

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/cdk/portal/dom-portal-outlet.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ export class DomPortalOutlet extends BasePortalOutlet {
8787
);
8888
this._appRef!.attachView(componentRef.hostView);
8989
this.setDisposeFn(() => {
90-
this._appRef!.detachView(componentRef.hostView);
90+
// Verify that the ApplicationRef has registered views before trying to detach a host view.
91+
// This check also protects the `detachView` from being called on a destroyed ApplicationRef.
92+
if (this._appRef!.viewCount > 0) {
93+
this._appRef!.detachView(componentRef.hostView);
94+
}
9195
componentRef.destroy();
9296
});
9397
}

0 commit comments

Comments
 (0)