Angular · Middle · Question 66 of 75

How do you give an InjectionToken a default value without adding providers?

export const PAGE_SIZE = new InjectionToken<number>('PAGE_SIZE', {
  providedIn: 'root',
  factory: () => 20,
});

Explanation

The factory can call inject() for other dependencies. Any provider for the token still overrides the default.

Further reading

Problem with a question

Angular · Middle · question 66

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz