Skip to content

Commit b22eddf

Browse files
committed
fix(core): Run component disposal before destroyRootHostView() to avoid crash if change detection is triggered.
Closes angular#5226
1 parent 857bef9 commit b22eddf

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

modules/angular2/src/core/linker/dynamic_component_loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ export class DynamicComponentLoader_ extends DynamicComponentLoader {
249249
var component = this._viewManager.getComponent(newLocation);
250250

251251
var dispose = () => {
252-
this._viewManager.destroyRootHostView(hostViewRef);
253252
if (isPresent(onDispose)) {
254253
onDispose();
255254
}
255+
this._viewManager.destroyRootHostView(hostViewRef);
256256
};
257257
return new ComponentRef_(newLocation, component, type, injector, dispose);
258258
});

modules/angular2/test/platform/bootstrap_spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import {IS_DART, isPresent, stringify} from 'angular2/src/facade/lang';
1414
import {bootstrap} from 'angular2/platform/browser';
1515
import {ApplicationRef} from 'angular2/src/core/application_ref';
16-
import {Component, Directive, View, platform} from 'angular2/core';
16+
import {Component, Directive, View, OnDestroy, platform} from 'angular2/core';
1717
import {BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS} from 'angular2/platform/browser';
1818
import {DOM} from 'angular2/src/core/dom/dom_adapter';
1919
import {DOCUMENT} from 'angular2/render';
@@ -67,6 +67,15 @@ class HelloRootMissingTemplate {
6767
class HelloRootDirectiveIsNotCmp {
6868
}
6969

70+
@Component({selector: 'hello-app'})
71+
@View({template: ''})
72+
class HelloOnDestroyTickCmp implements OnDestroy {
73+
appRef: ApplicationRef;
74+
constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; }
75+
76+
onDestroy(): void { this.appRef.tick(); }
77+
}
78+
7079
class _ArrayLogger {
7180
res: any[] = [];
7281
log(s: any): void { this.res.push(s); }
@@ -163,6 +172,15 @@ export function main() {
163172
});
164173
}));
165174

175+
it('should not crash if change detection is invoked when the root component is disposed',
176+
inject([AsyncTestCompleter], (async) => {
177+
bootstrap(HelloOnDestroyTickCmp, testProviders)
178+
.then((ref) => {
179+
expect(() => ref.dispose()).not.toThrow();
180+
async.done();
181+
});
182+
}));
183+
166184
it('should unregister change detectors when components are disposed',
167185
inject([AsyncTestCompleter], (async) => {
168186
var app = platform(BROWSER_PROVIDERS).application([BROWSER_APP_PROVIDERS, testProviders]);

0 commit comments

Comments
 (0)