无法将 JSON 数据绑定到 Angular2 nativescript 中的视图
Cant Bind JSON data to View in Angular2 nativescript
我调用了 API 并成功返回数据。
但是有了 JSON 数据,我无法绑定视图。
下面是 HTML 模板
<StackLayout>
<Label [text]="users.BookingReferenceID"> </Label>
</StackLayout>
这是我的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))
});
这是从 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}
我总是会在视图中得到 undefined..请帮助我
您正在 JSON.stringify
处理数据,这意味着您正在将 data
对象转换为字符串,而在 html 中您正在尝试 select string
s 字段类似于 object
.
从数据中删除 JSON.stringify
。
保持这样:
.subscribe((data) => this.users = data,
你的 html 应该是这样的:
<StackLayout>
<Label [text]="users?.Collection[0].BookingReferenceID"> </Label>
</StackLayout>
我调用了 API 并成功返回数据。 但是有了 JSON 数据,我无法绑定视图。 下面是 HTML 模板
<StackLayout>
<Label [text]="users.BookingReferenceID"> </Label>
</StackLayout>
这是我的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))
});
这是从 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}
我总是会在视图中得到 undefined..请帮助我
您正在 JSON.stringify
处理数据,这意味着您正在将 data
对象转换为字符串,而在 html 中您正在尝试 select string
s 字段类似于 object
.
从数据中删除 JSON.stringify
。
保持这样:
.subscribe((data) => this.users = data,
你的 html 应该是这样的:
<StackLayout>
<Label [text]="users?.Collection[0].BookingReferenceID"> </Label>
</StackLayout>