Skip to content

Commit 9be04f8

Browse files
andreialecualxhub
authored andcommitted
fix(upgrade): leak when angular1 destroys element
Fixes #6401 Closes #7935
1 parent 74e2bd7 commit 9be04f8

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

modules/angular2/src/upgrade/downgrade_ng2_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class DowngradeNg2ComponentAdapter {
160160
}
161161

162162
registerCleanup() {
163-
this.element.bind('$remove', () => this.viewManager.destroyRootHostView(this.hostViewRef));
163+
this.element.bind('$destroy', () => this.viewManager.destroyRootHostView(this.hostViewRef));
164164
}
165165
}
166166

modules/angular2/test/upgrade/upgrade_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,35 @@ export function main() {
230230
});
231231

232232
}));
233+
234+
it('should properly run cleanup when ng1 directive is destroyed',
235+
inject([AsyncTestCompleter], (async) => {
236+
var adapter: UpgradeAdapter = new UpgradeAdapter();
237+
var ng1Module = angular.module('ng1', []);
238+
var onDestroyed: EventEmitter<string> = new EventEmitter<string>();
239+
240+
ng1Module.directive('ng1', () => {
241+
return {
242+
template: '<div ng-if="!destroyIt"><ng2></ng2></div>',
243+
controller: function($rootScope, $timeout) {
244+
$timeout(function() { $rootScope.destroyIt = true; });
245+
}
246+
};
247+
});
248+
249+
var Ng2 = Component({selector: 'ng2', template: 'test'})
250+
.Class({
251+
constructor: function() {},
252+
ngOnDestroy: function() { onDestroyed.emit('destroyed'); }
253+
});
254+
ng1Module.directive('ng2', adapter.downgradeNg2Component(Ng2));
255+
var element = html('<ng1></ng1>');
256+
adapter.bootstrap(element, ['ng1'])
257+
.ready((ref) => {onDestroyed.subscribe(() => {
258+
ref.dispose();
259+
async.done();
260+
})});
261+
}));
233262
});
234263

235264
describe('upgrade ng1 component', () => {

0 commit comments

Comments
 (0)