fix(compiler): don't inject `viewProviders` into content child elements

E.g. in the following scenario,
`some-directive` should not be able to inject
any view provider that `my-comp-with-view-providers`
declares.

```
<my-comp-with-view-providers>
  <div some-directive></div>
</my-comp-with-view-providers>
```
This commit is contained in:
Tobias Bosch 2016-06-23 09:59:45 -07:00
parent 33a2f86b28
commit 9ed8f2d26e
2 changed files with 19 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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(
'<div simpleComponent><div needsService></div></div>',
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(