What are providers like useClass, useValue, useFactory and useExisting?
Model answer
They are ways to tell Angular how to create a dependency. useClass supplies a different class, such as a mock or an alternative implementation. useValue supplies a fixed value. useFactory runs a function, which can read other dependencies, to build the value. useExisting makes one token an alias for another provider, so both give the same instance.
They are useful for swapping implementations by environment, configuration and testing.
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?