0

I have a service, called ConfigService in config.serivce.ts, i need to get a function (getClientID) that returns a value and import this value into a module file: appAuth.module.ts

I need to know how to inject a value from a service function in such a file?

for example in my appAuth.module.ts:

 @NgModule({
   imports: [
    CommonModule,
    AuthModule.forRoot<any>({
     adapter: getClientID(),
    })],
   providers: [],
   declarations: []
    })
   export class AppAuthModule { }
Mo Halem
  • 184
  • 2
  • 12

1 Answers1

2

Wherever you need the result of getClientId() you should probably be injecting the ConfigService and call getClientId() instead of reading it from the environment.

As far as I know, your environment file shouldn't depend on anything. Instead, your Services, Components, etc. should depend on the environment file.

Here's an example from the Angular documentation on dependency injection. It shows how to inject (in this case) a UserService and a LoggerService into the UserContextService.

@Injectable({
  providedIn: 'root'
})
export class UserContextService {
  constructor(private userService: UserService, private loggerService: LoggerService) {
  }
}

See the Angular documentation for examples of how to use the environment file.

Joel
  • 261
  • 1
  • 6
  • I need to know how to inject the service in such a file? – Mo Halem Apr 19 '20 at 11:37
  • @MoHalem: I updated the answer with an example and a link to the dependency injection guide. – Joel Apr 19 '20 at 11:44
  • Yes i know this way is but this is for injecting into a class, what if i want to inject into an interface or a module? – Mo Halem Apr 19 '20 at 19:14
  • Could you give an example of what you are trying to achieve? An Interface has no need (and can't) inject anything, it only declares how something else (that implements it) can be used. – Joel Apr 20 '20 at 03:29
  • What is getClientID? Is it something that is global for the whole application? Where is the AuthModule coming from? How is it solved it in their documentation? – Joel Apr 23 '20 at 19:01