quoci
quoci

Reputation: 3557

How to call a service inside providers?

let's say I have a function called service(). This function needs to call a service to fetch some data. So anyone who knows how to call a service inside providers?

export function service(){
 // call service
}

@Component({
...
  providers: [{
    provide: ...,
    useValue: {
      test(): void {
        service();
      },
    },
  }],
})

Upvotes: 0

Views: 630

Answers (1)

ferhado
ferhado

Reputation: 2594

Check this examples: https://angular.io/guide/dependency-injection-providers

Here is an example how you can use it:

export const TestFunc = (service: YourService) => {
  return () => {
    service.callYourMethod();
  }
}

@NgModule({
  imports: [
     //...
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: TestFunc,
      multi: true,
      deps: [YourService]
     }
   ],
  bootstrap: [AppComponent]
})

Upvotes: 1

Related Questions