Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

486
Visualizações
What is the right way to use `useFactory` and `deps:[ ]` in Angular?

I want to use a simple provider with useFactory which will also use deps (learning purpose) .

So I've created this plnkr :

//app.module.ts
...
const randomFactory = (car,engine) => { return new Calc(car,engine);  };
 ...
 providers: [Car,Engine, 
             { provide: 'Random', 
               useFactory: randomFactory ,  
               deps: [Car, Engine],
             },
            ]

Where Calc is :

export class Calc
{
  car:Car;
  engine:Engine;

  constructor ( Engine engine, Car car)
   {
    this.car=car;
    this.engine=engine;
   }
  doSomething(){alert(this.car.getInfo() + this.engine.getInfo()}
}

Now I want to use it as an injected object so I did this :

export class AppComponent {
  constructor(@Inject('Random') r) {
    r.doSomething(); //should alert "car data + engine data" , but I get an error.
   }
}

But I get an error : car is not defined

Question:

I don't see any point of using deps:[] If I already declare that modules in the main app module ( and so it is known in the global app) and in the Calc class where I import those classes.

So how can I fix my code to use deps:[] the right way ?

plnkr

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The providers configuration is correct. You've got a syntax error in the Calc constructor. The correct declaration of the arguments is as follows:

constructor (engine: Engine, car: Car){

Here is the updated plunker:

https://plnkr.co/edit/HsECR0HEcIaPNbK6A7ZZ?p=preview

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda