I have an external API that returns a list. I have the following in service
load() {
let headers = new Headers();
headers.append("Authorization", Config.token);
return this._http.get(Config.apiUrl + "grocery/list", {headers: headers})
.map(res => res.json())
.map(data => {
let groceryList = [];
data.list.forEach((grocery) => {
groceryList.push(new Grocery(grocery._id, grocery.name));
});
return groceryList;
})
.catch(this.handleErrors);
}
I have the following code in component:
ngOnInit() {
this._groceryService.load()
.subscribe(loadedGroceries => {
loadedGroceries.forEach((groceryObject) => {
console.dir(groceryObject);
this.groceryItems.unshift(groceryObject);
});
});
// It works if I uncomment this. It will just render one item, still nothing from API
// this.groceryItems.unshift(new Grocery('iuiu', 'jjjjj'));
}
If I uncomment the final line above it is rendering on the ListView. My ListView is templated away and look like below:
<GridLayout>
<ListView [items]="groceryItems" class="small-spacing">
<template let-item="item">
<Label [text]="item.name" class="medium-spacing"></Label>
</template>
</ListView>
</GridLayout>
The API returns something like this:
{
"success": true,
"msg": "Lists found!",
"list": [
{
"_id": "574a324a18cadad00665a7f3",
"name": "Carrot",
"userid": "57482f309ae6c358703066ca",
"completed": 0,
"__v": 0
}
]
}