18 Using services

 

This book covers

  • Understanding services and the problems they solve
  • Defining and registering services
  • Consuming services using dependency injection
  • Using services in components, pipes, and directives
  • Using services to isolate components

Services are objects that provide common functionality to support other building blocks in an application, such as directives, components, and pipes. What’s important about services is the way that they are used, which is through a process called dependency injection. Using services can increase the flexibility and scalability of an Angular application, but dependency injection can be a difficult topic to understand. To that end, I start this chapter slowly and explain the problems that services and dependency injection can be used to solve, how dependency injection works, and why you should consider using services in your projects. Table 18.1 puts services in context.

18.1 Preparing the example project

18.2 Understanding the object distribution problem

18.2.1 Demonstrating the problem

18.2.2 Distributing objects as services using dependency injection

18.2.3 Declaring dependencies in other building blocks

18.3 Understanding the test isolation problem

18.3.1 Isolating components using services and dependency injection

18.4 Completing the adoption of services

18.4.1 Updating the root component and template

18.4.2 Updating the child components

18.5 Summary