Skip to main content
added 1 character in body
Source Link
Günter Zöchbauer
  • 661.4k
  • 235
  • 2.1k
  • 1.6k

In Angular2 services are singletons per provider. If you provide a service multiple times, you'll get multiple instanceinstances.

If you provide it in

@NgModule({ 
  providers: [SomeService]
  ...
})

then it will be provided at the root scope and you'll get a single instance for your whole application. Even when more than one @NgModule() contains a service in providers, you'll get only one instance because they are hoisted in the application root scope.

Lazy loaded modules have their own root scope.

If you provide a service in @Component() every such component instance and it's children will get a different service instance.

In Angular2 services are singletons per provider. If you provide a service multiple times, you'll get multiple instance.

If you provide it in

@NgModule({ 
  providers: [SomeService]
  ...
})

then it will be provided at the root scope and you'll get a single instance for your whole application. Even when more than one @NgModule() contains a service in providers you'll get only one instance because they are hoisted in the application root scope.

Lazy loaded modules have their own root scope.

If you provide a service in @Component() every such component instance and it's children will get a different service instance.

In Angular2 services are singletons per provider. If you provide a service multiple times, you'll get multiple instances.

If you provide it in

@NgModule({ 
  providers: [SomeService]
  ...
})

then it will be provided at the root scope and you'll get a single instance for your whole application. Even when more than one @NgModule() contains a service in providers, you'll get only one instance because they are hoisted in the application root scope.

Lazy loaded modules have their own root scope.

If you provide a service in @Component() every such component instance and it's children will get a different service instance.

Source Link
Günter Zöchbauer
  • 661.4k
  • 235
  • 2.1k
  • 1.6k

In Angular2 services are singletons per provider. If you provide a service multiple times, you'll get multiple instance.

If you provide it in

@NgModule({ 
  providers: [SomeService]
  ...
})

then it will be provided at the root scope and you'll get a single instance for your whole application. Even when more than one @NgModule() contains a service in providers you'll get only one instance because they are hoisted in the application root scope.

Lazy loaded modules have their own root scope.

If you provide a service in @Component() every such component instance and it's children will get a different service instance.