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.

Note

The code, links, and a live example of this are available at http://ngcookbook.herokuapp.com/1109/.

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 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.