Skip to content

Add ability to use inject() for element injector hierarchy #47566

@JeanMeche

Description

@JeanMeche

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When providing a Service in a component, inject() triggers NullInjectorError when called within runInContext :

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [Service],
})
export class AppComponent {
  public constructor(injector: EnvironmentInjector, service: Service) {
    console.log(service); // OK
    console.log(inject(Service)); //OK

    setTimeout(() => {
      injector.runInContext(() => {
        console.log(inject(Service)); //KO
      });
    }, 100);
  }
}

This does not happen

  • if the service is providedIn:'root'
  • if the service is provided in the module

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-yccqgi?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Please provide the environment you discovered this bug in (run ng version)

Angular 14.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: difeatureIssue that requests a new feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions