O'Reilly logo

Angular 2 Cookbook by Matt Frisbie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Controlling service instance creation and injection with NgModule

In a stark departure from Angular 1.x, Angular 2 features a hierarchical injection scheme. This has a substantial number of implications, and one of the more prominent one is the ability to control when, and how many, services are created.

Note

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

Getting ready

Suppose you begin with the following simple application:

[app/root.component.ts] import {Component} from '@angular/core'; @Component({ selector: 'root', template: ` <h1>root component!</h1> <article></article> <article></article> ` }) export class RootComponent {} [app/article.component.ts] import {Component} from '@angular/core'; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required