load external file on service

11 views
Skip to first unread message

marc

unread,
Aug 12, 2019, 10:32:48 AM8/12/19
to Angular and AngularJS discussion
Hi all,
I have a question for you. I should load an external file, a JSON file, containing environment variables.

For example:

export class ClientService {
 
...
 checkMail
(...): any {

 
return this.http.post(`${this.conf.hostServer}/api/otp`, ...)

 
}



In this case I have this.conf.hostServer that must be read from another class.

@Injectable()
export class ConfigService {
 env
: string
 constructor
(public http: HttpClient) {
 
this.setSocketServer();

 
}
 setSocketServer
() {
 
return new Promise<boolean>((resolve: (a: boolean) => void): void => {
 
this.http.get('./config.json').pipe(map((x: any) => {
 
this.env = x.env;
 resolve
(true);
 
}))
 
.subscribe();
 
});
 
}
}




In this way I have in my ClientService the correct variables but I have to wait in my components that they are ready.
How can I do this ?

I saw that is present another way using APP_INITIALIZE but I don't like it for this case.

What do you suggest me ?


Thanks.

Tito

unread,
Aug 12, 2019, 11:30:54 AM8/12/19
to Angular and AngularJS discussion
load it using your basic

const typeDefs = require("./config");
Reply all
Reply to author
Forward
0 new messages