Can a defined provider that is a fat arrow method call another provided fat arrow method?

122 Views Asked by At

Lets say i have 2 fat arrow methods defined in the providers array of @NgModule. They are called capitalize and capitalizeCallingMethod.

@NgModule({
providers: [
    { provide: 'capitalize', 
       useValue: (stringToCapitalize : string) => { 
       if(stringToCapitalize && stringToCapitalize.length > 1)
         return stringToCapitalize.charAt(0).toUpperCase() + stringToCapitalize.slice(1).toLowerCase();
       else 
         return stringToCapitalize;
       } 
    },
    { provide: 'capitalizeCallingMethod', 
       useValue: aString => {
           return capitalize(aString); //How do i call capitalize?
       } 
    }
  ]
});

Is it possible to call capitalize from within capitalizeCallingMethod and if so, how do i achieve this? I've already looked at similar questions on stackoverflow about providers calling providers but they are all about provider classes that use other provider classes.

1

There are 1 best solutions below

1
On

For the capitalizeCallingMethod provider, replace useValue with useFactory, and pass the capitalize token as a dependency

An example of the format looks like

{ 
    provide: HeroService,
    useFactory: heroServiceFactory,
    deps: [Logger, UserService]
  };