The following code is triggering NullInjectorError
when i'm calling @inject()
in runInContext
.
This does not happen if the service is providedIn: 'root'
or passed in the module providers
.
Any idea why ?
@Injectable()
class Service {}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [Service],
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
public constructor(service: Service, injector: EnvironmentInjector) {
console.log(service); // OK
console.log(inject(Service)); //OK
setTimeout(() => {
injector.runInContext(() => {
console.log(inject(Service)); //KO
});
}, 1000);
}
}
Per Angular#47566, this is currently as design but could be improved.
So wait & see.