angular2 call service via OnNgInit
If you need to call a service, please do it via OnNgInit instead of using constructor. If you doing unit testing, your code might not work.
As shown in the code below :
class SomeService implements OnInit { constructor(private http: Http) { } ngOnInit() { // this should hit mocked backend this.http.get("dummmy url").subscribe(v => { console.log('constructor subscribe hit'); }); } someMethod() { // this should hit mocked backend this.http.get("anotherurl").subscribe(v => { console.log('some method called subscribe hit'); }); } }
Comments