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?

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You can use angular 2 DI without the constructor check this stackoverflow.com/a/37812906/1401751

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.