What is the purpose of providedIn with the Injectable decorator when generating Services in Angular 6?

When generating services in the Angular CLI, it is adding extra metadata with a ‘provided in’ property with a default of ‘root’ for the Injectable decorator.

@Injectable({
  providedIn: 'root',
})

What exactly does providedIn do? I am assuming this is making the service available like a ‘global’ type singleton service for the whole application, however, wouldn’t be cleaner to declare such services in the provider array of the AppModule?

6 Answers
6

Leave a Comment