22

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 {
}
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
  • `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
  • very detailed answer > https://stackoverflow.com/a/72561645/5578092 – Ali Jun 10 '22 at 01:05

3 Answers3

25

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

ruth
  • 29,535
  • 4
  • 30
  • 57
akpgp
  • 761
  • 6
  • 10
  • 3
    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 – Muhammed Albarmavi Jan 24 '20 at 08:10
  • 1
    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 – Muhammed Albarmavi Jan 24 '20 at 08:14
  • 3
    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ödingcöder Feb 27 '20 at 16:16
  • 2
    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
15

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

maxisam
  • 21,975
  • 9
  • 75
  • 84
  • Thanks @malbarmavi , kudos to everyone who patriciates this thread. I had the same question like you as well. – maxisam Oct 03 '21 at 15:46
10

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

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
  • 4
    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? – Nick Wills Sep 05 '20 at 18:29
  • 2
    @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