Here is the code I am currently using:
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")}
);
}
Although this code works, I am struggling with passing the object RESPONSE to the variable ARRAYDATA
. Can someone please assist me? Here is the code that is not working as expected:
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) // shows undefined
}
I am in need of using the variable ARRAYDATA outside the function, for example:
constructor(private http: Http) {
this.getCards('data.json');
console.log(this.ARRAYDATA);
}