Anthony Davis Anthony Davis - 1 year ago 100
TypeScript Question

Angular Providers

Following the angular 2 tutorial @

In the


I inserted "
providers: [HeroService]
" which contains the

Created a constructor:

constructor(private heroService: HeroService) {}

Now the part I don't understand is how I am able to use


The only propertes defined in this class are:

title = 'Tour of Heroes';
heroes: Hero[];
selectedHero: Hero;

Does the providers in the
decorator automatically create a property to access it through

The App is working, just don't know how we magically were able to access heroService through

Answer Source

The private (could also be public) in

constructor(private heroService: HeroService) {}

also creates a property heroService and assigns the value passed to the constructor. That's a TypeScript feature and is not Angular2 or DI dependent.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download