11
votes

In Angular 9 the injectable decorator option providedIn has a new value called any. What is the difference between root and any?

Is a service considered a singleton in the case that I use any?

@Injectable({providedIn: 'any'})
class UsefulService {
}
3
any is a super confusing name (I'm sure there was about a month long discussion on the name though!) It's easier if you read providedIn in your head as provided by which ngModuleSimon_Weaver

3 Answers

14
votes

The difference between the root and any as per offical documentation :

  • root : The application-level injector in most apps.

  • platform : A special singleton platform injector shared by all applications on the page.

  • any : The NgModule injector that receives the resolution.

For more details please refer this article.

Is a service considered a singleton in the case that I use any? - No

4
votes

angular 9 introduce new option for injectable decorator ProvidedIn in addition to the previous root and module options, now we have two additional options platform , any

🔵 root— This tells Angular to provide the service in the application root level and the service will be created once (singleton service ) and provide the same instance in every module that injects the token.

🔵 any— Provides a unique instance in every module (including lazy modules) that injects the token.

🔵 platform— Specifying providedIn: 'platform' makes the service available in a special singleton platform injector that is shared by all applications on the page.

a detailed look at Angular's 'root' and 'any' provider scopes

1
votes

I think provided answers are not really clear. However, @jkonst, @schrödingcöder and @Bruce are correct in the comment.

For any one sent here by Google,

any doesn't Provides a unique instance in every module. (should say only in every shared module)

Which means one instance in every inject scope

from https://angular.io/guide/providers

With providedIn: 'any', all eagerly loaded modules share a singleton instance; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.

diagram