Angular Services

What are services?

  • ES6 classes
     
  • Singleton objects inside a certain scope
     
  • Accesible through Dependency Injection
     
  • Used to handle app logic and state
    • server requests
    • data validation
    • ...

Dependency Injection

Component 1

constructor:  serviceA

ei Angular,

I need access to serviceA!

Angular

here you are!

service A

Dependecy Injection idea

[ Create component ]

During App bootstrap

  • Angular creates an injector
     
  • The injector creates dependencies
     
  • And store them so it can reuse them
     
  • The provider tells the injector how to create a dependency

Root Module

 App

 Injector

dependencies:  serviceA(providerA),  serviceB(providerB),   . . .

service A

service B

Component 1

constructor:  serviceA

Component 3

dependencies: serviceC(providerC)

constructor:  serviceC, serviceB

 Injector

service C

Component 2

constructor:  serviceB

Made with Slides.com