Here are some example constructors from the Angular 2 docs:
export class AppComponent implements OnInit {
title = 'Tour of heroes';
heroes: Hero[];
selectedHero: Hero;
constructor(private heroService: HeroService) { }
getHeroes() {
this.HeroService.getHeroes().then(heroes => this.heroes = heroes);
}
}
and...
class Car {
constructor(engine, tires, doors){
this.engine = engine;
this.tires = tires;
this.doors = doors;
}
}
I don't understand Why and When to create a constructor()
in angular 2 /typescript (I've read the official documentation where they create a constructor for Dependency Injection and for Services).