Permalink
Browse files

fix(core): Injector correctly honors the @self flag (#24520)

Injector was incorrectly returning instance from parent injector even
when `@self` was specified.

PR Close #24520
  • Loading branch information...
mhevery committed Jun 14, 2018
1 parent 27bc7dc commit ccbda9de658cd685d947dfc2ac85664a09efb11a
Showing with 10 additions and 4 deletions.
  1. +2 −0 packages/core/src/view/ng_module.ts
  2. +8 −4 packages/core/test/view/ng_module_spec.ts
@@ -118,6 +118,8 @@ export function resolveNgModuleDep(
return (
data._providers[index] =
_createProviderInstance(data, data._def.providersByKey[depDef.tokenKey]));
} else if (depDef.flags & DepFlags.Self) {
return notFoundValue;
}
return data._parent.get(depDef.token, notFoundValue);
} finally {
@@ -80,11 +80,14 @@ class FromChildWithOptionalDep {
}
class FromChildWithSkipSelfDep {
constructor(public depFromParent: ChildDep|null, public depFromChild: Bar|null) {}
constructor(
public skipSelfChildDep: ChildDep|null, public selfChildDep: ChildDep|null,
public optionalSelfBar: Bar|null) {}
static ngInjectableDef: InjectableDef<FromChildWithSkipSelfDep> = defineInjectable({
factory: () => new FromChildWithSkipSelfDep(
inject(ChildDep, InjectFlags.SkipSelf|InjectFlags.Optional),
inject(Bar, InjectFlags.Self|InjectFlags.Optional)),
inject(ChildDep, InjectFlags.Self),
inject(Bar, InjectFlags.Self|InjectFlags.Optional), ),
providedIn: MyChildModule,
});
}
@@ -162,8 +165,9 @@ describe('NgModuleRef_ injector', () => {
it('injects skip-self and self deps across injectors properly', () => {
const instance = childRef.injector.get(FromChildWithSkipSelfDep);
expect(instance instanceof FromChildWithSkipSelfDep).toBeTruthy();
expect(instance.depFromParent).toBeNull();
expect(instance.depFromChild instanceof Bar).toBeTruthy();
expect(instance.skipSelfChildDep).toBeNull();
expect(instance.selfChildDep instanceof ChildDep).toBeTruthy();
expect(instance.optionalSelfBar).toBeNull();
});
it('does not inject something not scoped to the module',

0 comments on commit ccbda9d

Please sign in to comment.