1

When I call my service injected in the constructor, I get undefined. the service is called in ngOnInit method, From Difference between Constructor and ngOnInit I have seen that constructor run first , but in my case I noted the opposite, so I'm bit confused. have someone more explication about that, thanks.

constructor(private curveService  :ProgressCurveService, private util : UtilService) {
    this.startPickerOptions = new DatePickerOptions();
    this.endPickerOptions = new DatePickerOptions();
    //this.datePickerOptions.initialDate = new Date(Date.now());
   }

ngOnInit() {
    this.curveService.instance.getCurve(this.startDate.formatted,this.endDate.formatted,this.amplutid).
    then(res => {

        this.lineChartLabels = this.util.dateToShortString(Object.keys(res.progressPlotData))
        this.lineChartData = this.util.objectToIntArray(res.progressPlotData);
    }).catch(res => console.log('error if date selecting ...'));
}

progress curve service:

import { progressCurveItf } from './progress-curve/progress-curve-interface';

@Injectable()
export class ProgressCurveService {

    state : string = 'project';
  constructor(private prsCurve : PrsProgressCurveService, private projCurve : ProjProgressCurveService) { }
  get instance():progressCurveItf{

    if(this.state == 'subproject'){
        return this.prsCurve;
    } else {
        return this.projCurve;
    }
  } 

}
Community
  • 1
  • 1
mustafa918
  • 498
  • 1
  • 6
  • 15

2 Answers2

0

While you're returning an instance of type progressCurveItf that is an interface I think there is something wrong with the instantiation of what you're returning, check if you provide PrsProgressCurveService and ProjProgressCurveService.

mustafa918
  • 498
  • 1
  • 6
  • 15
0

To answer your question Constructor will get invoked since it belongs to ES6, basically which got the first priority. Where as ngOnInit is a life cycle hook designed by angular team which will get invoked after constructor even after ngOnChanges life cycle hook.

Constructor -> ngOnChanges -> ngOnInit -> followed by other life cycle hooks