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

Popular posts from this blog

OpenCover code coverage for .Net Core

ionic2 cordova build android - Unable resolve gradle 2.2.3

Using Custom DLL with IronPython / Scripts