2

Is it possible to instantiate Http Service manually without having it to put as a constructor argument?

export class SimpleGridServer {
    private http: Http;
    constructor(public options: SimpleServerData) {
        http = new Http(.../* Argument here */);
    }
}

To instantiate this class.

var grid = new SimpleGridServer({/* options */});

I want to instantiate this class without having a dependency on Http service for my component that import SimpleGridServer. If this is possible, what is the drawback for this scenario?

jmvtrinidad
  • 3,347
  • 3
  • 22
  • 42

1 Answers1

4

If this is possible, what is the drawback for this scenario?

You need to be in an Angular DI workflow to use Angular stuff.

You can get a handle to angular's injector directly:

import {ReflectiveInjector} from '@angular/core';
import {HTTP_PROVIDERS, Http, Headers} from "@angular/http";
const injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
const http = injector.get(Http);
basarat
  • 261,912
  • 58
  • 460
  • 511