Skip to content

Commit 685cc26

Browse files
pkozlowski-opensourcevicb
authored andcommitted
fix(common): don't recreate view when context shape doesn't change (angular#18277)
Problem description: when using ngTemplateOutlet with context as an object literal in a template and binding to the context's property the embedded view would get re-created even if context object remains essentially the same (the same shape, just update to one properties). This happens since currently change detection will re-create object references when an object literal is used and one of its properties gets updated through a binding. Solution: this commit changes ngTemplateOutlet logic so we take context object shape into account before deciding if we should re-create view or just update existing context. Fixes angular#13407
1 parent 5b7432b commit 685cc26

File tree

2 files changed

+151
-11
lines changed

2 files changed

+151
-11
lines changed

packages/common/src/directives/ng_template_outlet.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, EmbeddedViewRef, Input, OnChanges, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';
9+
import {Directive, EmbeddedViewRef, Input, OnChanges, SimpleChange, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';
1010

1111
/**
1212
* @ngModule CommonModule
@@ -49,13 +49,58 @@ export class NgTemplateOutlet implements OnChanges {
4949
set ngOutletContext(context: Object) { this.ngTemplateOutletContext = context; }
5050

5151
ngOnChanges(changes: SimpleChanges) {
52-
if (this._viewRef) {
53-
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef));
52+
const recreateView = this._shouldRecreateView(changes);
53+
54+
if (recreateView) {
55+
if (this._viewRef) {
56+
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef));
57+
}
58+
59+
if (this.ngTemplateOutlet) {
60+
this._viewRef = this._viewContainerRef.createEmbeddedView(
61+
this.ngTemplateOutlet, this.ngTemplateOutletContext);
62+
}
63+
} else {
64+
if (this._viewRef && this.ngTemplateOutletContext) {
65+
this._updateExistingContext(this.ngTemplateOutletContext);
66+
}
5467
}
68+
}
69+
70+
/**
71+
* We need to re-create existing embedded view if:
72+
* - templateRef has changed
73+
* - context has changes
74+
*
75+
* To mark context object as changed when the corresponding object
76+
* shape changes (new properties are added or existing properties are removed).
77+
* In other words we consider context with the same properties as "the same" even
78+
* if object reference changes (see https://github.com/angular/angular/issues/13407).
79+
*/
80+
private _shouldRecreateView(changes: SimpleChanges): boolean {
81+
const ctxChange = changes['ngTemplateOutletContext'];
82+
return !!changes['ngTemplateOutlet'] || (ctxChange && this._hasContextShapeChanged(ctxChange));
83+
}
84+
85+
private _hasContextShapeChanged(ctxChange: SimpleChange): boolean {
86+
const prevCtxKeys = Object.keys(ctxChange.previousValue || {});
87+
const currCtxKeys = Object.keys(ctxChange.currentValue || {});
88+
89+
if (prevCtxKeys.length === currCtxKeys.length) {
90+
for (let propName of currCtxKeys) {
91+
if (prevCtxKeys.indexOf(propName) === -1) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
} else {
97+
return true;
98+
}
99+
}
55100

56-
if (this.ngTemplateOutlet) {
57-
this._viewRef = this._viewContainerRef.createEmbeddedView(
58-
this.ngTemplateOutlet, this.ngTemplateOutletContext);
101+
private _updateExistingContext(ctx: Object): void {
102+
for (let propName of Object.keys(ctx)) {
103+
(<any>this._viewRef.context)[propName] = (<any>this.ngTemplateOutletContext)[propName];
59104
}
60105
}
61106
}

packages/common/test/directives/ng_template_outlet_spec.ts

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {CommonModule} from '@angular/common';
10-
import {Component, ContentChildren, Directive, NO_ERRORS_SCHEMA, QueryList, TemplateRef} from '@angular/core';
10+
import {Component, ContentChildren, Directive, Injectable, NO_ERRORS_SCHEMA, OnDestroy, QueryList, TemplateRef} from '@angular/core';
1111
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
1212
import {expect} from '@angular/platform-browser/testing/src/matchers';
1313

@@ -26,11 +26,9 @@ export function main() {
2626

2727
beforeEach(() => {
2828
TestBed.configureTestingModule({
29-
declarations: [
30-
TestComponent,
31-
CaptureTplRefs,
32-
],
29+
declarations: [TestComponent, CaptureTplRefs, DestroyableCmpt],
3330
imports: [CommonModule],
31+
providers: [DestroyedSpyService]
3432
});
3533
});
3634

@@ -125,9 +123,105 @@ export function main() {
125123
fixture.componentInstance.context = {shawshank: 'was here'};
126124
detectChangesAndExpectText('was here');
127125
}));
126+
127+
it('should update but not destroy embedded view when context values change', () => {
128+
const template =
129+
`<ng-template let-foo="foo" #tpl><destroyable-cmpt></destroyable-cmpt>:{{foo}}</ng-template>` +
130+
`<ng-template [ngTemplateOutlet]="tpl" [ngTemplateOutletContext]="{foo: value}"></ng-template>`;
131+
132+
fixture = createTestComponent(template);
133+
const spyService = fixture.debugElement.injector.get(DestroyedSpyService);
134+
135+
detectChangesAndExpectText('Content to destroy:bar');
136+
expect(spyService.destroyed).toBeFalsy();
137+
138+
fixture.componentInstance.value = 'baz';
139+
detectChangesAndExpectText('Content to destroy:baz');
140+
expect(spyService.destroyed).toBeFalsy();
141+
});
142+
143+
it('should recreate embedded view when context shape changes', () => {
144+
const template =
145+
`<ng-template let-foo="foo" #tpl><destroyable-cmpt></destroyable-cmpt>:{{foo}}</ng-template>` +
146+
`<ng-template [ngTemplateOutlet]="tpl" [ngTemplateOutletContext]="context"></ng-template>`;
147+
148+
fixture = createTestComponent(template);
149+
const spyService = fixture.debugElement.injector.get(DestroyedSpyService);
150+
151+
detectChangesAndExpectText('Content to destroy:bar');
152+
expect(spyService.destroyed).toBeFalsy();
153+
154+
fixture.componentInstance.context = {foo: 'baz', other: true};
155+
detectChangesAndExpectText('Content to destroy:baz');
156+
expect(spyService.destroyed).toBeTruthy();
157+
});
158+
159+
it('should destroy embedded view when context value changes and templateRef becomes undefined',
160+
() => {
161+
const template =
162+
`<ng-template let-foo="foo" #tpl><destroyable-cmpt></destroyable-cmpt>:{{foo}}</ng-template>` +
163+
`<ng-template [ngTemplateOutlet]="value === 'bar' ? tpl : undefined" [ngTemplateOutletContext]="{foo: value}"></ng-template>`;
164+
165+
fixture = createTestComponent(template);
166+
const spyService = fixture.debugElement.injector.get(DestroyedSpyService);
167+
168+
detectChangesAndExpectText('Content to destroy:bar');
169+
expect(spyService.destroyed).toBeFalsy();
170+
171+
fixture.componentInstance.value = 'baz';
172+
detectChangesAndExpectText('');
173+
expect(spyService.destroyed).toBeTruthy();
174+
});
175+
176+
it('should not try to update null / undefined context when context changes but template stays the same',
177+
() => {
178+
const template = `<ng-template let-foo="foo" #tpl>{{foo}}</ng-template>` +
179+
`<ng-template [ngTemplateOutlet]="tpl" [ngTemplateOutletContext]="value === 'bar' ? null : undefined"></ng-template>`;
180+
181+
fixture = createTestComponent(template);
182+
detectChangesAndExpectText('');
183+
184+
fixture.componentInstance.value = 'baz';
185+
detectChangesAndExpectText('');
186+
});
187+
188+
it('should not try to update null / undefined context when template changes', () => {
189+
const template = `<ng-template let-foo="foo" #tpl1>{{foo}}</ng-template>` +
190+
`<ng-template let-foo="foo" #tpl2>{{foo}}</ng-template>` +
191+
`<ng-template [ngTemplateOutlet]="value === 'bar' ? tpl1 : tpl2" [ngTemplateOutletContext]="value === 'bar' ? null : undefined"></ng-template>`;
192+
193+
fixture = createTestComponent(template);
194+
detectChangesAndExpectText('');
195+
196+
fixture.componentInstance.value = 'baz';
197+
detectChangesAndExpectText('');
198+
});
199+
200+
it('should not try to update context on undefined view', () => {
201+
const template = `<ng-template let-foo="foo" #tpl>{{foo}}</ng-template>` +
202+
`<ng-template [ngTemplateOutlet]="value === 'bar' ? null : undefined" [ngTemplateOutletContext]="{foo: value}"></ng-template>`;
203+
204+
fixture = createTestComponent(template);
205+
detectChangesAndExpectText('');
206+
207+
fixture.componentInstance.value = 'baz';
208+
detectChangesAndExpectText('');
209+
});
128210
});
129211
}
130212

213+
@Injectable()
214+
class DestroyedSpyService {
215+
destroyed = false;
216+
}
217+
218+
@Component({selector: 'destroyable-cmpt', template: 'Content to destroy'})
219+
class DestroyableCmpt implements OnDestroy {
220+
constructor(private _spyService: DestroyedSpyService) {}
221+
222+
ngOnDestroy(): void { this._spyService.destroyed = true; }
223+
}
224+
131225
@Directive({selector: 'tpl-refs', exportAs: 'tplRefs'})
132226
class CaptureTplRefs {
133227
@ContentChildren(TemplateRef) tplRefs: QueryList<TemplateRef<any>>;
@@ -137,6 +231,7 @@ class CaptureTplRefs {
137231
class TestComponent {
138232
currentTplRef: TemplateRef<any>;
139233
context: any = {foo: 'bar'};
234+
value = 'bar';
140235
}
141236

142237
function createTestComponent(template: string): ComponentFixture<TestComponent> {

0 commit comments

Comments
 (0)