10

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 {
}
malbarmavi
  • 18,795
  • 4
  • 45
  • 73
  • `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 ngModule` – Simon_Weaver Feb 19 '20 at 18:17

2 Answers2

13

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

Michael D
  • 20,838
  • 4
  • 12
  • 37
akpgp
  • 569
  • 4
  • 10
  • is it correct to say in every module I use the service that has `any` option I will have a new instance of that service – malbarmavi Jan 24 '20 at 08:10
  • yes it is correct. Please mark this as answer if it was helpful. – akpgp Jan 24 '20 at 08:12
  • the article has fully information about the new options `any` and `platform` , thanks – malbarmavi Jan 24 '20 at 08:14
  • 1
    Seems that the quoted article states for "any" that "All eagerly loaded modules share one instance provided by the root module injector." Presumably this means that only a lazy module will have its new instance. – schrödingercöder Feb 27 '20 at 16:16
  • It is not correct to say that in every module you have a new instance of that service. Eagerly loaded modules share one instance. This is explaned also in angular documentation: https://angular.io/guide/providers – jkonst Sep 23 '20 at 16:54
3

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

malbarmavi
  • 18,795
  • 4
  • 45
  • 73
  • 2
    According to Angular documentation, "any" will provides a unique instance in lazy modules only but all eagerly loaded modules will share a singleton instance. So how can it be that "any" provides a unique instance in EVERY module? – Bruce Sep 05 '20 at 18:29
  • 1
    @Bruce, you are correct. It's an instance per module injector scope. Basically one instance per runtime bundle. – Lars Gyrup Brink Nielsen Oct 04 '20 at 20:30