David Harel
David Harel

Reputation: 49

typescript Angular2 Instantiating Injectable such as Http outside the constructor

I want to create a class that uses a private Http object and should be encapsulated. I do not want my class to be Injectable.

If my class has a constructor such as: constructor(private _http: Http) {} then the caller must provide the Http object.

Looking at the examples in https://angular.io/docs/ts/latest/tutorial/toh-pt6.html I could not find how to create an Http object outside the constructor.

What is the solution for such a request?

Thanks.

Upvotes: 0

Views: 431

Answers (1)

Phil Cap
Phil Cap

Reputation: 209

You do not need to manually create an Http object to achieve your goal. The caller can obtain the Http object through dependency injection and pass it to your special class. This does not violate the conditions that you have mentioned.

On a side note you can create an Http instance manually by supplying an appropriate ConnectionBackend and RequestOptions.

Upvotes: 0

Related Questions