I am new to angular2 typescript and rxjs and observables .
I am trying to get information from api using this code
let Alertsanswer = this.LogService.getAlertsForClient(this.userId);
var a = Alertsanswer.subscribe((response) => {
this.alerts=JSON.parse(response)
console.log(this.alerts) //give the right response.
},
error => {
var err = error;
alert(err);
}
)
as you can see inside the subscribe I initialize a private variable: "this.alerts"
outside the subscribe this variable has undefined value console.log(this.alerts) //give undefined.
Using this.alert in component doom is undefined too this give an error:
<div>{{alerts}}</div>
first question: why ""this.alerts inside subscribe give right value to the console but outside (including the doom) is always undefined.
second question: I understand the the value coming from the server is async, if I have further code that relay on the answer where would I write it(callback), would it be inside the subscribe? what is the meaning of subscribe? thanks for any help