I'm using this code:
private ARRAYDATA: any[];
constructor(private http: Http) {
this.getCards('data.json');
}
getCards(url)
{
this.http.get(url)
.map(response => response.json())
.subscribe(
function(response) {
console.log(response); //show object
},
function(error) { console.log("Error happened" + error)},
function() { console.log("the subscription is completed")}
);
}
This code works, now I can not understand how to pass an object RESPONSE to a variable ARRAYDATA; Help me please!
This code does not work:
getCards(url)
{
this.http.get(url)
.map(response => response.json())
.subscribe(
function(response) {
console.log(response); //show object
this.arraydata = response;
},
function(error) { console.log("Error happened" + error)},
function() { console.log("the subscription is completed")}
);
console.log(this.arraydata)// show undefind
}
I need to use a variable ARRAYDATA outside the function. right here
constructor(private http: Http) {
this.getCards('data.json');
console.log(this.ARRAYDATA);
}
ARRAYDATAinside the constructor like so. Like you can see from the duplicate, data is ONLY available inside the subscription. That's just how it is, you cannot do anything about it. You need to manipulate the data inside the callback once you have received it :)