1

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.

4
  • Now I don't understand what returns undefined exactly. The call to _ListService.getList(), or is it _ListService that is undefined (thus throwing an error at runtime, not returning anything) ? Commented Jan 31, 2019 at 10:41
  • @Pac0 added list.service.ts Commented Jan 31, 2019 at 10:41
  • GET list.url.com/undefined 404 (Not Found) <= Error message Commented Jan 31, 2019 at 10:43
  • Thank you, much clearer now. Indeed Mohan's answer's right, it was a problem of order of operations. Commented Jan 31, 2019 at 12:46

2 Answers 2

4

ngOnInit will get called after the constructor and the properties like tools are initialized. You will have to rework the logic to initialize tools after initializing the Globals. May be you can try by calling setEnv in the constructor instead of ngOnInit

Sign up to request clarification or add additional context in comments.

2 Comments

Good to know it worked for you! Can you accept this as an answer?
I did, had to wait 5 minutes.
2

Trty to move your this._ListService.getList(this.globals.env); into constructor or ngOnInit

  import {Component, Inject, OnInit} from '@angular/core';
    import {ListService} from '../../assets/list/list.service';
    import {Globals} from '../globals';

        export class CtiNavComponent implements OnInit {
            tools; 
            ngOnInit() {
                this.globals.setEnv('int');
                console.log(this.globals.env);  //<= returns int
            }
            constructor(private _ListService: ListService, private globals: Globals) {
            this._ListService.getList(this.globals.env);  //try to move here
        }
        }

3 Comments

The constructor is called before ngOnInit I believe, so this would produce the same result. I think getList should be called inside ngOnInit after the setEnv call.
try to move it to ngOnInit and say me what you get
@AntonSkybun had to move getList into ngOnInit, but it didn't work without MOHAN's solution. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.