Skip to content

Commit 70efee5

Browse files
jelbournkara
authored andcommitted
chore: support AppRef attachView/detachView (#1894)
1 parent 4331b27 commit 70efee5

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/lib/core/portal/dom-portal-host.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,36 @@ export class DomPortalHost extends BasePortalHost {
4343
} else {
4444
componentRef = componentFactory.create(portal.injector || this._defaultInjector);
4545

46-
// When creating a component outside of a ViewContainer, we need to manually register
47-
// its ChangeDetector with the application. This API is unfortunately not yet published
48-
// in Angular core. The change detector must also be deregistered when the component
49-
// is destroyed to prevent memory leaks.
50-
//
51-
// See https://github.com/angular/angular/pull/12674
52-
let changeDetectorRef = componentRef.changeDetectorRef;
53-
(this._appRef as any).registerChangeDetector(changeDetectorRef);
54-
55-
this.setDisposeFn(() => {
56-
(this._appRef as any).unregisterChangeDetector(changeDetectorRef);
57-
58-
// Normally the ViewContainer will remove the component's nodes from the DOM.
59-
// Without a ViewContainer, we need to manually remove the nodes.
60-
let componentRootNode = this._getComponentRootNode(componentRef);
61-
if (componentRootNode.parentNode) {
62-
componentRootNode.parentNode.removeChild(componentRootNode);
63-
}
64-
65-
componentRef.destroy();
66-
});
46+
// ApplicationRef's attachView and detachView methods are in Angular ^2.2.1 but not before.
47+
// The `else` clause here can be removed once 2.2.1 is released.
48+
if ((this._appRef as any)['attachView']) {
49+
(this._appRef as any).attachView(componentRef.hostView);
50+
51+
this.setDisposeFn(() => {
52+
(this._appRef as any).detachView(componentRef.hostView);
53+
componentRef.destroy();
54+
});
55+
} else {
56+
// When creating a component outside of a ViewContainer, we need to manually register
57+
// its ChangeDetector with the application. This API is unfortunately not published
58+
// in Angular <= 2.2.0. The change detector must also be deregistered when the component
59+
// is destroyed to prevent memory leaks.
60+
let changeDetectorRef = componentRef.changeDetectorRef;
61+
(this._appRef as any).registerChangeDetector(changeDetectorRef);
62+
63+
this.setDisposeFn(() => {
64+
(this._appRef as any).unregisterChangeDetector(changeDetectorRef);
65+
66+
// Normally the ViewContainer will remove the component's nodes from the DOM.
67+
// Without a ViewContainer, we need to manually remove the nodes.
68+
let componentRootNode = this._getComponentRootNode(componentRef);
69+
if (componentRootNode.parentNode) {
70+
componentRootNode.parentNode.removeChild(componentRootNode);
71+
}
72+
73+
componentRef.destroy();
74+
});
75+
}
6776
}
6877

6978
// At this point the component has been instantiated, so we move it to the location in the DOM

0 commit comments

Comments
 (0)