Cant Bind JSON data to View in Angular2 nativescript

大兔子大兔子 提交于 2019-12-11 06:31:53

问题


I Have called a API and returned data successfully. But with that JSON data, I cant bind with the view. Below is the HTML Template

<StackLayout>            
        <Label [text]="users.BookingReferenceID"> </Label>
</StackLayout>    

This is my app.component.ts

this._dataService.Search(id)            
            .subscribe((data) => this.users = JSON.stringify(data),
             error => console.log(error),
             () => {(
                   console.log('Data Retrieved successfully' + this.users))   
             });

This is the data returned from the api

{"Collection":  
 [{"IsIndexData":false,
"DocumentNumber":"2300000002018",
"BookingReferenceID":"CID0EX",
"IssuingDate":"2016-11-04T00:00:00",
"Errors":null,"Information":null,
"Warnings":null}],
"Errors":null,
"Information":null,
"Warnings":null}

Always I will get undefined in the view..Please help me


回答1:


You are JSON.stringifying the data which means that you are turning your data object into a string and inside the html you are trying to select the strings field like an object.

Remove the JSON.stringify from data.

Leave it like this:

.subscribe((data) => this.users = data,

And your html should be something like:

<StackLayout>            
        <Label [text]="users?.Collection[0].BookingReferenceID"> </Label>
</StackLayout>


来源:https://stackoverflow.com/questions/41315078/cant-bind-json-data-to-view-in-angular2-nativescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!