2

According to angular.io , it says calling server on constructor is a bad idea and we should do it on other hookup methods of component lifecycle. Why is it so ? . is it about clean separation of responsibility ? Code:

ngOnInit():void{
 this.heroes=this.heroService.getHeroes(); //better
}


constructor(private heroService:HeroService){

  this.heroes=this.heroService.getHeroes(); //this is bad
}
PaladiN
  • 4,625
  • 8
  • 41
  • 66
aspxsushil
  • 514
  • 1
  • 5
  • 16
  • 3
    According to Angular.io, author said that writing code on `ngOnInit` is much easier for testing also. Ref: https://angular.io/docs/ts/latest/tutorial/toh-pt4.html#!#the-ngoninit-lifecycle-hook – Manh Le Jan 04 '17 at 03:30

0 Answers0