wrote some code after following this (https://www.bennettnotes.com/post/angular-read-system-environment-variables/). I added API_URL in my system variable
Not sure if it will help if I create stackblitz. I tried to put as much as code below as I can.
It reads the system variable, but not at the very first page. (Note: In my local, it returns correct url even first time, I see this issue in DEV server)
Lets say I have a Home page(http://my-dev-url/home) and Configuration page (http://my-dev-url/config).
When I open chrome browser and enter Home page url, it (environment.API_URL) returns undefined. -> Now I click on Config hyper link, it picks the correct API_URL from system env. -> If I click on Home page link, it works with correct API_URL.
If I open chrome browser and enter config page url first time, it (environment.API_URL) returns undefined. -> Now I click on Home page hyper link, it picks the correct API_URL from system env. -> Now if I click on Config page link, it works with correct API_URL.
Code: (apart from what is already described in above url) environment.ts
export const environment = {
production: false,
dev: false,
qa: false,
local: true,
API_URL: $ENV.API_URL
};
home.component.ts:
ngOnInit() {
this.homeService
.getTv(this.localIp)
.subscribe((result) => {
console.log(result);
});
}
}
home.service.ts:
import { environment } from '../../environments/environment';
this.http.get(environment.API_URL + '/api/tv/' + this.localIp ).toPromise().then(response => {
const data = response.json() as Tv;
this.tv=data;
config.service.ts:
import { environment } from '../../environments/environment';
this.http.get(environment.API_URL + '/api/' + 'config/1').toPromise().then(response => {
const data = response.json();
}).catch(error => {
alert(error);
});
Any clue what am I doing wrong.