diff --git a/modules/@angular/compiler/src/view_compiler/compile_element.ts b/modules/@angular/compiler/src/view_compiler/compile_element.ts index c7318806df..d43022e269 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_element.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_element.ts @@ -313,6 +313,14 @@ export class CompileElement extends CompileNode { } // access regular providers on the element if (isBlank(result)) { + let resolvedProvider = this._resolvedProviders.get(dep.token); + // don't allow directives / public services to access private services. + // only components and private services can access private services. + if (resolvedProvider && (requestingProviderType === ProviderAstType.Directive || + requestingProviderType === ProviderAstType.PublicService) && + resolvedProvider.providerType === ProviderAstType.PrivateService) { + return null; + } result = this._instances.get(dep.token); } } diff --git a/modules/@angular/core/test/linker/view_injector_integration_spec.ts b/modules/@angular/core/test/linker/view_injector_integration_spec.ts index 9236c39fee..804fdcf920 100644 --- a/modules/@angular/core/test/linker/view_injector_integration_spec.ts +++ b/modules/@angular/core/test/linker/view_injector_integration_spec.ts @@ -397,7 +397,7 @@ export function main() { expect(created).toBe(true); })); - it('should not instantiate other directives that depend on viewProviders providers', + it('should not instantiate other directives that depend on viewProviders providers (same element)', fakeAsync(() => { expect( () => createComp( @@ -407,6 +407,16 @@ export function main() { .toThrowError(/No provider for service!/); })); + it('should not instantiate other directives that depend on viewProviders providers (child element)', + fakeAsync(() => { + expect( + () => createComp( + '
', + tcb.overrideViewProviders( + SimpleComponent, [{provide: 'service', useValue: 'service'}]))) + .toThrowError(/No provider for service!/); + })); + it('should instantiate directives that depend on providers of other directives', fakeAsync(() => { var el = createComp(