Skip to content

Commit

Permalink
fix(core): properly destroy embedded Views attatched to ApplicationRef (
Browse files Browse the repository at this point in the history
#13459)

Fixes #13062
  • Loading branch information
pkozlowski-opensource authored and vicb committed Dec 15, 2016
1 parent 94b7031 commit d40bbf4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion modules/@angular/core/src/linker/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ export abstract class AppView<T> {
} else {
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 index = projectedViews.indexOf(this);
// perf: pop is faster than splice!
Expand Down
21 changes: 19 additions & 2 deletions modules/@angular/core/test/application_ref_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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 {ErrorHandler} from '@angular/core/src/error_handler';
import {ComponentRef} from '@angular/core/src/linker/component_factory';
Expand Down Expand Up @@ -275,9 +275,15 @@ export function main() {
vc: ViewContainerRef;
}

@Component({template: '<template #t>Dynamic content</template>'})
class EmbeddedViewComp {
@ViewChild(TemplateRef)
tplRef: TemplateRef<Object>;
}

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MyComp, ContainerComp],
declarations: [MyComp, ContainerComp, EmbeddedViewComp],
providers: [{provide: ComponentFixtureNoNgZone, useValue: true}]
});
});
Expand Down Expand Up @@ -321,6 +327,17 @@ export function main() {
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',
() => {
const comp = TestBed.createComponent(MyComp);
Expand Down

0 comments on commit d40bbf4

Please sign in to comment.