0

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.

Maurice
  • 6,698
  • 9
  • 47
  • 104

1 Answers1

0

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]
  };
Drenai
  • 11,315
  • 9
  • 48
  • 82
  • this requires creating an extra class. The question is whether its possible to call a fat arrow function from another fat arrow function. – Maurice May 02 '21 at 19:45
  • It's an example only, you can use the same approach without creatng a class – Drenai May 02 '21 at 19:46