title: injector.get giving undefined
body: <p>I am working with Angular 4, using authservice and http service.</p>
  
  <p>When am trying to refresh jwt token, by checking token expiration before each request to server. To avoid cyclic dependency I am using injector in auth.service.ts as:</p>
  
  <pre><code>constructor(// private http: HttpClient,
  inj: Injector) {
  setTimeout(() =&gt; { this.http = inj.get(HttpClient) })
  }
  </code></pre>
  
  <p>but when am calling http.somefunction() getting error as
  ERROR TypeError: Cannot read property 'somefunction' of undefined</p>
  
  <p>my http.service.ts is as follows</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, Headers } from '@angular/http';
  import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
  import { JwtAuthService } from './auth.service'
  @Injectable()
  export class HttpClient {
  jwtHelper: JwtHelper = new JwtHelper();
  constructor(private http: Http, private _jwtAuthService: JwtAuthService) { }
  
  createAuthorizationHeader(headers: Headers) {
      let token = '';
      if (sessionStorage.getItem('token')) {
          token = sessionStorage.getItem('token')
      }
  
      if (token &amp;&amp; typeof token === 'string') {
          if (this.jwtHelper.isTokenExpired(token)) {
              // get aws token using refresh_token and get jwt token
              this._jwtAuthService.refreshToken().then(refreshedtoken =&gt; {
                  headers.append('Authorization', 'Bearer ' + refreshedtoken);
              });
          }
          else {
              headers.append('Authorization', 'Bearer ' + token);
          }
  
  
      }
  
  }
  getwithheader(url, header) {
      const headers = new Headers();
      headers.append('Authorization', 'Bearer ' + header);
      this.createAuthorizationHeader(headers);
      return this.http.get(url, {
          headers: headers
      });
  }
  get(url) {
      const headers = new Headers();
      this.createAuthorizationHeader(headers);
      return this.http.get(url, {
          headers: headers
      });
  }
  awspost(url, data) {
      const headers = new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      return this.http.post(url, data, {
          headers: headers
      });
  }
  post(url, data) {
      const headers = new Headers();
      this.createAuthorizationHeader(headers);
      return this.http.post(url, data, {
          headers: headers
      });
  }
  }
  </code></pre>
  
  <p>and auth servce is as follows</p>
  
  <pre><code>import { Injectable, Injector } from '@angular/core';
  import { SharedService } from './shared.service';
  import { ErrorHandlerService } from './error-handler.service';
  import { HttpClient } from './http.service';
  import { Observable } from 'rxjs/rx';
  import { Http, Response } from '@angular/http';
  import * as AWS from 'aws-sdk/global';
  import 'rxjs/add/operator/toPromise';
  import { environment } from '../../environments/environment';
  import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
  @Injectable()
  export class JwtAuthService {
  BASE_URL = this._sharedService.BASE_URL;
  AWS_ACCESS_KEY = 'test';
  http: HttpClient;
    jwtHelper: JwtHelper = new JwtHelper();
  constructor(// private http: HttpClient,
      private _sharedService: SharedService,
      private _errorHandler: ErrorHandlerService,
      inj: Injector) {
      setTimeout(() =&gt; { this.http = inj.get(HttpClient) })
  }
  
  getAwsToken(code): any {
      const body = 'grant_type=' + environment.AWS_GRANT_TYPE + '&amp;client_id=' + environment.AWS_CLIENT_ID + '&amp;redirect_uri=' + environment.AWS_REDIRECT_URI + '&amp;code_verifier=' + code;
      return this.http.awspost(environment.AWS_BASE_URL + environment.AWS_TOKEN_END_POINT + '?code=' + code, body).toPromise()
          .then((resp: Response) =&gt; resp.text())
          .catch(err =&gt; this._errorHandler.handleError(err));
  }
  getToken(token): any {
      return this.http.getwithheader(this.BASE_URL + '/token', token).toPromise()
          .then((resp: Response) =&gt; resp.text())
          .catch(err =&gt; this._errorHandler.handleError(err));
  }
  getRefreshAWSToken(refreshToken): any {
      const body = 'grant_type=refresh_token' + '&amp;client_id=' + environment.AWS_CLIENT_ID + '&amp;redirect_uri=' + environment.AWS_REDIRECT_URI + '&amp;code_verifier=' + refreshToken;
      return this.http.awspost(environment.AWS_BASE_URL + environment.AWS_TOKEN_END_POINT, body).toPromise()
          .then((resp: Response) =&gt; resp.text())
          .catch(err =&gt; this._errorHandler.handleError(err));
  }
  refreshToken(): any {
      this.getRefreshAWSToken(sessionStorage.getItem('refreshToken'))
          .then(resp =&gt; {
              this.getToken(JSON.parse(resp).id_token)
                  .then(jwtresp =&gt; {
                      sessionStorage.setItem('user', JSON.stringify(this.jwtHelper.decodeToken(jwtresp)));
                      sessionStorage.setItem('token', JSON.parse(jwtresp).token);
                      return JSON.parse(jwtresp).token
                  });
          });
  }
  }
  </code></pre>
  
  <p>What am missing here?</p>