Service injection aliasing with useClass and useExisting

As your application becomes more complex, you may come to a situation where you would like to use your services in a polymorphic style. More specifically, some places in your application may want to request Service A, but a configuration somewhere in your application will actually give it Service B. This recipe will demonstrate one way in which this can be useful, but this behavior allows your application to be more extensible in multiple ways.


The code, links, and a live example of this are available at

Getting ready

Suppose you begin with the following skeleton application.

Dual services

You begin with two services, ArticleService and EditorArticleService ...

Get Angular 2 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.