Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Why does constructor of HttpClient in Angular not need param, when I instanstiate it via constructor of other class, but needs via new?

I need to make a static method for instanstiating an object of class, but faced an issue.

import { HttpClient } from '@angular/common/http';

export MyClass {
  // Case 1
  public static init(): MyClass {
    return this(new HttpClient(***), ....); // Need to pass new HttpClient() instead ***
  }

  // Case 2
  public constructor(private http: HttpClient, ....) {} // Need to pass nothing
}

Why I have to pass argument in case 1, but in case 2 not? And how to solve my problem?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Answer

This happens thanks to dependency injection. Angular says:

Dependency injection, or DI, is a design pattern in which a class requests dependencies from external sources rather than creating them

In your case you don't need to "create" that resource but simply use it, and for that dependency injection comes to your aid

I recommend you take a look at the official documentation of Angular, always very clear and quite useful!

And how to solve my problem?

If you just need to use HttpClient, do as in // Case 2 and without the need to initialize anything; otherwise, if you can please explain better what your goal is and how you can't achieve it so we can help you, thanks!


Let me know if you have any doubts ;)

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!