Skip to content

Commit

Permalink
fix(common): ngTemplateOutlet modifies initial context when getting n…
Browse files Browse the repository at this point in the history
…ew context

Closes angular#24515
  • Loading branch information
divdavem committed Jun 14, 2018
1 parent d8f7b29 commit 0dfe4ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/common/src/directives/ng_template_outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class NgTemplateOutlet implements OnChanges {

if (this.ngTemplateOutlet) {
this._viewRef = this._viewContainerRef.createEmbeddedView(
this.ngTemplateOutlet, this.ngTemplateOutletContext);
this.ngTemplateOutlet, Object.assign({}, this.ngTemplateOutletContext));
}
} else {
if (this._viewRef && this.ngTemplateOutletContext) {
Expand Down
16 changes: 16 additions & 0 deletions packages/common/test/directives/ng_template_outlet_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
detectChangesAndExpectText('was here');
}));

it('should not change the previous context when receiving a new context', () => {
const template =
`<ng-template let-content="content" #tpl>{{content}}</ng-template>` +
`<ng-container *ngTemplateOutlet="tpl; context: context"></ng-container>`;
fixture = createTestComponent(template);

const initialContext = {content: 'first'};
fixture.componentInstance.context = initialContext;
detectChangesAndExpectText('first');

fixture.componentInstance.context = {content: 'second'};
detectChangesAndExpectText('second');

expect(initialContext.content).toBe('first');
});

it('should update but not destroy embedded view when context values change', () => {
const template =
`<ng-template let-foo="foo" #tpl><destroyable-cmpt></destroyable-cmpt>:{{foo}}</ng-template>` +
Expand Down

0 comments on commit 0dfe4ad

Please sign in to comment.