What does the useValue key do?
The useValue key tells Angular:
"Use this specific value as the dependency, do not create anything."
Example:
ts
providers: [
{ provide: 'API_URL', useValue: 'https://api.example.com' }
]Now, if 'API_URL' is requested somewhere, Angular simply supplies this string.
How to use it:
ts
constructor(@Inject('API_URL') private apiUrl: string) {
console.log(this.apiUrl); // logs: https://api.example.com
}When it is used:
- for simple constants (strings, numbers, objects);
- for configuration and settings;
- to avoid creating a separate service.
Summary:
useValue is a way to register a ready-made value in the DI system so Angular supplies it by token without creating any objects.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.