fix(dynamic_component_loader): check whether the dynamically loaded component has already been destroyed

Fixes #2748
Closes #2767
This commit is contained in:
Tobias Bosch 2015-06-29 10:03:14 -07:00
parent da4de21f28
commit d6cef88dd8
2 changed files with 37 additions and 2 deletions

View File

@ -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);
});

View File

@ -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: '<child-cmp *ng-if="ctxBoolProp"></child-cmp>',
directives: [NgIf, ChildComp]
}))
.overrideView(
ChildComp,
new viewAnn.View(
{template: '<location #loc></location>', 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],