Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda