What are injection tokens, and when would you use InjectionToken?
Model answer
Dependency injection looks up a provider by a token, which is usually a class. For values that are not classes, such as a config object, a string or an interface, create an InjectionToken and provide a value for it: { provide: API_URL, useValue: 'https://api.example.com' }, then inject(API_URL).
This keeps settings out of the code that uses them and lets tests or different environments provide different values.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?