From 71007ef9b2936b5404bef1d5642437e8dab1a71c Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Tue, 7 Aug 2018 17:36:23 +0300 Subject: [PATCH] refactor(upgrade): share code for destroying upgraded components between `dynamic` and `static` (#25357) PR Close #25357 --- packages/upgrade/src/common/upgrade_helper.ts | 7 +++++++ packages/upgrade/src/dynamic/upgrade_ng1_adapter.ts | 8 +------- packages/upgrade/src/static/upgrade_component.ts | 5 +---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/upgrade/src/common/upgrade_helper.ts b/packages/upgrade/src/common/upgrade_helper.ts index c31ba7eb1c..8782de9c9e 100644 --- a/packages/upgrade/src/common/upgrade_helper.ts +++ b/packages/upgrade/src/common/upgrade_helper.ts @@ -119,6 +119,13 @@ export class UpgradeHelper { return this.compileHtml(template); } + onDestroy($scope: angular.IScope, controllerInstance?: any) { + if (controllerInstance && isFunction(controllerInstance.$onDestroy)) { + controllerInstance.$onDestroy(); + } + $scope.$destroy(); + } + prepareTransclusion(): angular.ILinkFn|undefined { const transclude = this.directive.transclude; const contentChildNodes = this.extractChildNodes(); diff --git a/packages/upgrade/src/dynamic/upgrade_ng1_adapter.ts b/packages/upgrade/src/dynamic/upgrade_ng1_adapter.ts index b31657bc7b..f52e178640 100644 --- a/packages/upgrade/src/dynamic/upgrade_ng1_adapter.ts +++ b/packages/upgrade/src/dynamic/upgrade_ng1_adapter.ts @@ -262,13 +262,7 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck { } } - ngOnDestroy() { - if (this.controllerInstance && isFunction(this.controllerInstance.$onDestroy)) { - this.controllerInstance.$onDestroy(); - } - - this.componentScope.$destroy(); - } + ngOnDestroy() { this.helper.onDestroy(this.componentScope, this.controllerInstance); } setComponentProperty(name: string, value: any) { this.destinationObj ![this.propertyMap[name]] = value; diff --git a/packages/upgrade/src/static/upgrade_component.ts b/packages/upgrade/src/static/upgrade_component.ts index a63699f8b4..73b7771b37 100644 --- a/packages/upgrade/src/static/upgrade_component.ts +++ b/packages/upgrade/src/static/upgrade_component.ts @@ -217,10 +217,7 @@ export class UpgradeComponent implements OnInit, OnChanges, DoCheck, OnDestroy { if (isFunction(this.unregisterDoCheckWatcher)) { this.unregisterDoCheckWatcher(); } - if (this.controllerInstance && isFunction(this.controllerInstance.$onDestroy)) { - this.controllerInstance.$onDestroy(); - } - this.$componentScope.$destroy(); + this.helper.onDestroy(this.$componentScope, this.controllerInstance); } private initializeBindings(directive: angular.IDirective) {