fix(core): properly destroy embedded Views attatched to ApplicationRef (#13459)
Fixes #13062
This commit is contained in:
parent
a0a05041ac
commit
3edca4d37e
|
@ -191,7 +191,8 @@ export abstract class AppView<T> {
|
||||||
} else {
|
} else {
|
||||||
this._renderDetach();
|
this._renderDetach();
|
||||||
}
|
}
|
||||||
if (this.declaredViewContainer && this.declaredViewContainer !== this.viewContainer) {
|
if (this.declaredViewContainer && this.declaredViewContainer !== this.viewContainer &&
|
||||||
|
this.declaredViewContainer.projectedViews) {
|
||||||
const projectedViews = this.declaredViewContainer.projectedViews;
|
const projectedViews = this.declaredViewContainer.projectedViews;
|
||||||
const index = projectedViews.indexOf(this);
|
const index = projectedViews.indexOf(this);
|
||||||
// perf: pop is faster than splice!
|
// perf: pop is faster than splice!
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, CompilerFactory, Component, NgModule, PlatformRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
|
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, CompilerFactory, Component, NgModule, PlatformRef, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
|
||||||
import {ApplicationRef, ApplicationRef_} from '@angular/core/src/application_ref';
|
import {ApplicationRef, ApplicationRef_} from '@angular/core/src/application_ref';
|
||||||
import {ErrorHandler} from '@angular/core/src/error_handler';
|
import {ErrorHandler} from '@angular/core/src/error_handler';
|
||||||
import {ComponentRef} from '@angular/core/src/linker/component_factory';
|
import {ComponentRef} from '@angular/core/src/linker/component_factory';
|
||||||
|
@ -275,9 +275,15 @@ export function main() {
|
||||||
vc: ViewContainerRef;
|
vc: ViewContainerRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Component({template: '<template #t>Dynamic content</template>'})
|
||||||
|
class EmbeddedViewComp {
|
||||||
|
@ViewChild(TemplateRef)
|
||||||
|
tplRef: TemplateRef<Object>;
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [MyComp, ContainerComp],
|
declarations: [MyComp, ContainerComp, EmbeddedViewComp],
|
||||||
providers: [{provide: ComponentFixtureNoNgZone, useValue: true}]
|
providers: [{provide: ComponentFixtureNoNgZone, useValue: true}]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -321,6 +327,17 @@ export function main() {
|
||||||
expect(appRef.viewCount).toBe(0);
|
expect(appRef.viewCount).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should detach attached embedded views if they are destroyed', () => {
|
||||||
|
const comp = TestBed.createComponent(EmbeddedViewComp);
|
||||||
|
const appRef: ApplicationRef = TestBed.get(ApplicationRef);
|
||||||
|
const embeddedViewRef = comp.componentInstance.tplRef.createEmbeddedView({});
|
||||||
|
|
||||||
|
appRef.attachView(embeddedViewRef);
|
||||||
|
embeddedViewRef.destroy();
|
||||||
|
|
||||||
|
expect(appRef.viewCount).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('should not allow to attach a view to both, a view container and the ApplicationRef',
|
it('should not allow to attach a view to both, a view container and the ApplicationRef',
|
||||||
() => {
|
() => {
|
||||||
const comp = TestBed.createComponent(MyComp);
|
const comp = TestBed.createComponent(MyComp);
|
||||||
|
|
Loading…
Reference in New Issue