How to bind complex object to the view in nativescript angular
Hi, I am getting complex object from backend like: var users :
[
{
"lastname": "Raboy",
"address": [
{
"city": "San Francisco",
"state": "CA",
"country": "USA"
},
{
"city": "San Francisco1",
"state": "CA1",
"country": "USA1"
}
]
},
{
"lastname": "Raboy2",
"address": [
{
"city": "San Francisco2",
"state": "CA2",
"country": "USA2"
},
{
"city": "San Francisco3",
"state": "CA3",
"country": "USA3"
}
]
}
]
I am binding view like this
<StackLayout>
<Label [text]="users[0].lastname" textWrap="true"></Label>
<Label [text]="users[0].address[1].country" textWrap="true"></Label>
</StackLayout>
[http://www.jsoneditoronline.org/?id=3cefaca86526c011bddd9e394c289f84]
With the static data we can able to load view easily, but my users object is getting constructed in ngonInit() with backend web api call, my livesync giving error as "lastname" of undefined. Is my view loading before completion of ngonInit() ??
Yes. Its trying to get the value users[0].lastname before the call would complete so very undefined. A bad way of fixing it might be: users[0]?.lastname.
I would create a component for it (that understands one object (input property), then implement it in the listview/ngfor).
listview: http://docs.nativescript.org/angular/ui/list-view.html ngfor: https://angular.io/cheatsheet
and use ngif to check for nulls.
This seems more like a stack overflow problem though.