fix(dynamic_component_loader): check whether the dynamically loaded component has already been destroyed
Fixes #2748 Closes #2767
This commit is contained in:
parent
da4de21f28
commit
d6cef88dd8
@ -73,7 +73,9 @@ export class DynamicComponentLoader {
|
|||||||
|
|
||||||
var dispose = () => {
|
var dispose = () => {
|
||||||
var index = viewContainer.indexOf(hostViewRef);
|
var index = viewContainer.indexOf(hostViewRef);
|
||||||
viewContainer.remove(index);
|
if (index !== -1) {
|
||||||
|
viewContainer.remove(index);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return new ComponentRef(newLocation, component, dispose);
|
return new ComponentRef(newLocation, component, dispose);
|
||||||
});
|
});
|
||||||
|
@ -15,10 +15,12 @@ import {
|
|||||||
viewRootNodes,
|
viewRootNodes,
|
||||||
TestComponentBuilder,
|
TestComponentBuilder,
|
||||||
RootTestComponent,
|
RootTestComponent,
|
||||||
inspectElement
|
inspectElement,
|
||||||
|
By
|
||||||
} from 'angular2/test_lib';
|
} from 'angular2/test_lib';
|
||||||
|
|
||||||
import {Injector} from 'angular2/di';
|
import {Injector} from 'angular2/di';
|
||||||
|
import {NgIf} from 'angular2/directives';
|
||||||
import {Component, View, onDestroy} from 'angular2/annotations';
|
import {Component, View, onDestroy} from 'angular2/annotations';
|
||||||
import * as viewAnn from 'angular2/src/core/annotations_impl/view';
|
import * as viewAnn from 'angular2/src/core/annotations_impl/view';
|
||||||
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
|
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',
|
it('should update host properties',
|
||||||
inject(
|
inject(
|
||||||
[DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter],
|
[DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user