Chapter 7. Services, Dependency Injection, and NgModule

This chapter will cover the following recipes:

  • Injecting a simple service into a component
  • Controlling service instance creation and injection with NgModule
  • Service injection aliasing with useClass and useExisting
  • Injecting a value as a service with useValue and OpaqueTokens
  • Building a provider-configured service with useFactory

Introduction

Angular 1 gave you a hodgepodge of different service types. Many of them had a great deal of overlap. Many of them were confusing. And all of them were singletons.

Angular 2 has totally thrown away this concept. In its place, there is a shiny new dependency injection system that is far more extensible and sensible than its predecessor. It allows you to have atomic ...

Get Angular 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.