Inject resolves dependencies within a supported injection context. It is not a general-purpose service locator callable from any later callback.
Before you start
You should understand a component’s class, template and dependencies. Follow both the framework lifecycle and the ownership of each value or resource. Apply examples inside an Angular application; the surrounding application configuration and imports depend on the feature being demonstrated.
The practical goal is to reason through this situation: Resolve a service during field initialization or construction in a created Angular class. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Resolve during supported creation
Field initialization or construction of an Angular-created class can use inject within a supported injection context.
Step 2: Capture the dependency
Store the service reference and use that reference in later event handlers or delayed callbacks.
Step 3: Preserve injector scope
If explicit context execution is necessary, choose the intended injector deliberately; do not turn inject into an arbitrary global lookup.
Worked scenario
Resolve a service during field initialization or construction in a created Angular class.
private readonly audit = inject(AuditService);
submit() {
this.audit.record('submit');
}Import inject from @angular/core and provide the application-defined AuditService. Resolution happens at instance creation; submit uses the already-resolved service rather than calling inject in the later callback.
Common mistake
Calling inject inside an arbitrary button handler can occur outside an injection context.
Verify the behavior
Invoke the action after component creation. Verify the injected instance belongs to the expected scope and no late injection-context error occurs.
Interview exercise
Use a dependency in a delayed callback.
Answer and reasoning
Capture the injected dependency beforehand or deliberately use a supported context mechanism; preserve the intended injector scope.
Continue learning
Compare the scenario with the Angular interview questions and test your understanding with the Angular MCQs. For terminology and implementation details, consult the reference material.