asp.net mvc - Angular 2 strings remain undefined on first iteration -


i'm trying fetch object server via http.get , works strings remain undefined on first iteration. the full object

the integers ok, problem strings.

        this.http.get("/home/getroom", { headers: headers }).map(res => res.json()).subscribe(q => {     this.timecount = q.timeperquestion; this.player1id = json.stringify(q.player1id); this.player2id = q.player2id; }, err => console.error(err),         () => console.log('complete')); 

after first iteration of function strings shown.

edit:

this class:

class appcomponent implements afterviewinit { public answered = false; public title = "loading question..."; public options = []; public correctanswer = false; public working = false; public timecount=15; public timer; public roomid; public player1id; public player2id;  constructor( @inject(http) private http: http, private ref: changedetectorref) {   }   nextquestion() {      this.working = true;     this.answered = false;     this.title = "loading question...";     this.options = [];     var headers = new headers();     headers.append('if-modified-since', 'mon, 27 mar 1972 00:00:00 gmt');     this.http.get("/home/getroom", { headers: headers }).map(res => res.json()).subscribe(q => {     this.timecount = q.timeperquestion;     this.player1id = json.stringify(q.player1id);     this.ref.detectchanges();     },         err => console.error(err),         () => console.log('complete')); 

edit 2: ok seems ok strings, when try display {{player1id}} shows right value on first iteration. "problem" console.log(player1id) used check string with, works after 1 iteration. thank trying help!

you force change detection run using changedetectorref class. inject , call detectchanges method:

constructor(private changedetectorref:changedetectorref) { }  somemethod() {   this.http.get("/home/getroom", { headers: headers }).map(res => res.json()).subscribe(q => {     this.timecount = q.timeperquestion;     this.player1id = json.stringify(q.player1id);     this.player2id = q.player2id;     this.changedetectorref.detectchanges();   },   err => console.error(err),   () => console.log('complete')); } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -