diff --git a/modules/@angular/compiler/src/template_parser/template_parser.ts b/modules/@angular/compiler/src/template_parser/template_parser.ts index 489564a82d..a197e9c09d 100644 --- a/modules/@angular/compiler/src/template_parser/template_parser.ts +++ b/modules/@angular/compiler/src/template_parser/template_parser.ts @@ -15,12 +15,12 @@ import {Parser} from '../expression_parser/parser'; import {ListWrapper, SetWrapper, StringMapWrapper} from '../facade/collection'; import {BaseException} from '../facade/exceptions'; import {isBlank, isPresent} from '../facade/lang'; +import {Identifiers, identifierToken} from '../identifiers'; import * as html from '../ml_parser/ast'; import {HtmlParser, ParseTreeResult} from '../ml_parser/html_parser'; import {expandNodes} from '../ml_parser/icu_ast_expander'; import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {mergeNsAndName, splitNsName} from '../ml_parser/tags'; -import {Identifiers, identifierToken} from '../identifiers'; import {ParseError, ParseErrorLevel, ParseSourceSpan} from '../parse_util'; import {ProviderElementContext, ProviderViewContext} from '../provider_analyzer'; import {ElementSchemaRegistry} from '../schema/element_schema_registry'; @@ -32,6 +32,7 @@ import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventA import {PreparsedElementType, preparseElement} from './template_preparser'; + // Group 1 = "bind-" // Group 2 = "var-" // Group 3 = "let-" diff --git a/modules/@angular/core/src/linker/template_ref.ts b/modules/@angular/core/src/linker/template_ref.ts index 7d5bb7d0a8..c18ebc9dcc 100644 --- a/modules/@angular/core/src/linker/template_ref.ts +++ b/modules/@angular/core/src/linker/template_ref.ts @@ -6,14 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ -import {isBlank} from '../facade/lang'; import {AppElement} from './element'; import {ElementRef} from './element_ref'; import {AppView} from './view'; import {EmbeddedViewRef} from './view_ref'; -const EMPTY_CONTEXT = new Object(); - /** * Represents an Embedded Template that can be used to instantiate Embedded Views. * @@ -51,10 +48,7 @@ export class TemplateRef_ extends TemplateRef { createEmbeddedView(context: C): EmbeddedViewRef { var view: AppView = this._viewFactory( this._appElement.parentView.viewUtils, this._appElement.parentInjector, this._appElement); - if (isBlank(context)) { - context = EMPTY_CONTEXT; - } - view.create(context, null, null); + view.create(context || {}, null, null); return view.ref; } diff --git a/modules/@angular/core/test/linker/integration_spec.ts b/modules/@angular/core/test/linker/integration_spec.ts index f871a91502..06a9077cb0 100644 --- a/modules/@angular/core/test/linker/integration_spec.ts +++ b/modules/@angular/core/test/linker/integration_spec.ts @@ -470,6 +470,27 @@ function declareTests({useJit}: {useJit: boolean}) { }); })); + it('should not share empty context for template directives - issue #10045', + inject( + [TestComponentBuilder, AsyncTestCompleter], + (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { + tcb.overrideView( + MyComp, new ViewMetadata({ + template: + '', + directives: [PollutedContext, NoContext] + })) + + .createAsync(MyComp) + .then((fixture) => { + + fixture.detectChanges(); + expect(fixture.debugElement.nativeElement).toHaveText('baz'); + + async.done(); + }); + })); + it('should not detach views in ViewContainers when the parent view is destroyed.', inject( [TestComponentBuilder, AsyncTestCompleter], @@ -2303,6 +2324,21 @@ class SomeViewport { } } +@Directive({selector: '[pollutedContext]'}) +class PollutedContext { + constructor(private tplRef: TemplateRef, private vcRef: ViewContainerRef) { + const evRef = this.vcRef.createEmbeddedView(this.tplRef); + evRef.context.bar = 'baz'; + } +} + +@Directive({selector: '[noContext]'}) +class NoContext { + constructor(private tplRef: TemplateRef, private vcRef: ViewContainerRef) { + this.vcRef.createEmbeddedView(this.tplRef); + } +} + @Pipe({name: 'double'}) class DoublePipe implements PipeTransform, OnDestroy { ngOnDestroy() {}