I'm trying to set my Global variable to a different value based on a string when I open my application.
globals.ts
import { Injectable } from '@angular/core';
@Injectable()
export class Globals {
env: string;
constructor() {}
setEnv(setEnv: string) {
this.env = setEnv;
}
}
cti-nav.component.ts
import {Component, Inject, OnInit} from '@angular/core';
import {ListService} from '../../assets/list/list.service';
import {Globals} from '../globals';
export class CtiNavComponent implements OnInit {
tools = this._ListService.getList(this.globals.env); //<= returns undefined
ngOnInit() {
this.globals.setEnv('int');
console.log(this.globals.env); //<= returns int
}
constructor(private _ListService: ListService, private globals: Globals) {}
}
cti-nav.module.ts
import {Globals} from '../globals';
@NgModule({
providers: [Globals]
})
export class CtiNavModule {}
list.service.ts
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class ListService {
constructor(private http: HttpClient) {}
getList(env) {
return this.http.get('https://list.url.com/' + env);
}
}
When I check the log it outputs int correctly and then in the _ListService part, it returns undefined. Correct would be if _ListService also returns int.
undefinedexactly. The call to_ListService.getList(), or is it_ListServicethat is undefined (thus throwing an error at runtime, not returning anything) ?