Search code examples
angulartypescriptangular-servicesangular-dependency-injection

What is the difference between providedIn any and root


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 {
}

Solution

  • 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