resource-in-injection-context
May 5, 2026 · View on GitHub
Checks that resource() is called inside an injection context, or that an explicit Injector is provided in the first argument, to avoid the NG0203 runtime error.
Configuration
- in the recommended preset (see the README for the configuration)
- or just this rule:
{
"rules": {
"angular-eslint-injection-context/resource-in-injection-context": "error"
}
}
Documentation
❌ Invalid
All the invalid cases are without an injector. See the valid cases below to see an example of how to provide an explicit Injector.
- in lifecycle methods, notably
ngOnInit
@Component()
export class ProductPage implements OnInit {
ngOnInit(): void {
resource({
loader: () => getProductPromise(),
});
}
}
- in any methods other than the constructor
@Component({
template: `<form (submit)="save()"></form>`
})
export class ProductEditPage {
save(): void {
resource({
loader: () => saveProductPromise(),
});
}
}
- in callbacks
@Component()
export class ProductPage implements OnInit {
ngOnInit(): void {
somePromise().then(() => {
resource({
loader: () => getProductPromise(),
});
}).catch(() => {});
}
}
Note
The rule reports both on asynchronous and synchronous callbacks, see the known limitation documentation.
- after awaiting (which is equivalent to be in a
.then()callback)
@Component()
export class ProductEditPage {
async save(): Promise<void> {
await somePromise();
resource({
loader: () => getProductPromise(),
});
}
}
- in non-Angular classes
export class Product {
constructor() {
resource({
loader: () => getProductPromise(),
});
}
}
- in standalone functions
function someFunction(): void {
resource({
loader: () => getProductPromise(),
});
}
✅ Valid
- in constructors of components, directives, pipes and injectables/services
@Component()
export class ProductsPage {
constructor() {
resource({
loader: () => getProductPromise(),
});
}
}
- in property initializers of components, directives, pipes and injectables/services
@Component()
export class ProductPage {
private readonly productResourceRef = resource({
loader: () => getProductPromise(),
});
}
- when providing an explicit
Injector
@Component()
export class ProductPage implements OnInit {
private readonly injector = inject(Injector);
ngOnInit(): void {
resource({
loader: () => getProductPromise(),
injector: this.injector,
});
}
}
Note
Prefer a literal object as in this example. If the second argument is a variable, the lint rule will not check if injector is actually present, see the known limitation documentation.
- in explicit injection context
@Injectable({ providedIn: 'root' })
export class MyService {
private readonly environmentInjector = inject(EnvironmentInjector);
someMethod() {
runInInjectionContext(this.environmentInjector, () => {
resource({
loader: () => getDataPromise(),
});
});
}
}
Note
The rule only detects runInInjectionContext() or TestBed.runInInjectionContext() in the current function, see the known limitation documentation.
- when asserted
function customOperator(injector: Injector) {
if (!injector) {
assertInInjectionContext(customOperator);
}
resource({
loader: () => getDataPromise(),
...(injector ? { injector } : {}),
});
}