diff --git a/modules/angular2/src/core/compiler/dynamic_component_loader.ts b/modules/angular2/src/core/compiler/dynamic_component_loader.ts
index 6a57be3d53..7da92d3d4a 100644
--- a/modules/angular2/src/core/compiler/dynamic_component_loader.ts
+++ b/modules/angular2/src/core/compiler/dynamic_component_loader.ts
@@ -73,7 +73,9 @@ export class DynamicComponentLoader {
var dispose = () => {
var index = viewContainer.indexOf(hostViewRef);
- viewContainer.remove(index);
+ if (index !== -1) {
+ viewContainer.remove(index);
+ }
};
return new ComponentRef(newLocation, component, dispose);
});
diff --git a/modules/angular2/test/core/compiler/dynamic_component_loader_spec.ts b/modules/angular2/test/core/compiler/dynamic_component_loader_spec.ts
index 24801b3a8f..c50be62362 100644
--- a/modules/angular2/test/core/compiler/dynamic_component_loader_spec.ts
+++ b/modules/angular2/test/core/compiler/dynamic_component_loader_spec.ts
@@ -15,10 +15,12 @@ import {
viewRootNodes,
TestComponentBuilder,
RootTestComponent,
- inspectElement
+ inspectElement,
+ By
} from 'angular2/test_lib';
import {Injector} from 'angular2/di';
+import {NgIf} from 'angular2/directives';
import {Component, View, onDestroy} from 'angular2/annotations';
import * as viewAnn from 'angular2/src/core/annotations_impl/view';
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
@@ -66,6 +68,37 @@ export function main() {
});
}));
+ it('should allow to dispose even if the location has been removed',
+ inject([DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter],
+ (loader, tcb: TestComponentBuilder, async) => {
+ tcb.overrideView(MyComp, new viewAnn.View({
+ template: '',
+ directives: [NgIf, ChildComp]
+ }))
+ .overrideView(
+ ChildComp,
+ new viewAnn.View(
+ {template: '', directives: [Location]}))
+ .createAsync(MyComp)
+ .then((tc) => {
+ tc.componentInstance.ctxBoolProp = true;
+ tc.detectChanges();
+ var childCompEl = tc.query(By.css('child-cmp'));
+ loader.loadIntoLocation(DynamicallyLoaded, childCompEl.elementRef, 'loc')
+ .then(ref => {
+ expect(tc.nativeElement).toHaveText("Location;DynamicallyLoaded;");
+
+ tc.componentInstance.ctxBoolProp = false;
+ tc.detectChanges();
+ expect(tc.nativeElement).toHaveText("");
+
+ ref.dispose();
+ expect(tc.nativeElement).toHaveText("");
+ async.done();
+ });
+ });
+ }));
+
it('should update host properties',
inject(
[DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter],