fix(upgrade): correctly destroy nested downgraded component (#22400)

Previously, when a downgraded component was destroyed in a way that did
not trigger the `$destroy` event on the element (e.g. when a parent
element was removed from the DOM by Angular, not AngularJS), the
`ComponentRef` was not destroyed and unregistered.
This commit fixes it by listening for the `$destroy` event on both the
element and the scope.

Fixes #22392

PR Close #22400
This commit is contained in:
George Kalpakas
2018-02-23 15:18:21 +02:00
committed by Alex Eagle
parent 83b32a0a0a
commit 8a85888773
6 changed files with 121 additions and 10 deletions

View File

@ -209,12 +209,16 @@ export class DowngradeComponentAdapter {
registerCleanup() {
const destroyComponentRef = this.wrapCallback(() => this.componentRef.destroy());
let destroyed = false;
this.element.on !('$destroy', () => {
this.componentScope.$destroy();
this.componentRef.injector.get(TestabilityRegistry)
.unregisterApplication(this.componentRef.location.nativeElement);
destroyComponentRef();
this.element.on !('$destroy', () => this.componentScope.$destroy());
this.componentScope.$on('$destroy', () => {
if (!destroyed) {
destroyed = true;
this.componentRef.injector.get(TestabilityRegistry)
.unregisterApplication(this.componentRef.location.nativeElement);
destroyComponentRef();
}
});
}