【发布时间】:2016-04-29 18:04:59
【问题描述】:
使用 Angular 2,我从服务接收 JSON 数据。像这样的:
{
"customerName": "foo",
"customerAddress": "123 Somewhere",
"products":
[
{
"productName": "bar",
"productNumber": 123
},
{
"productName": "baz",
"productNumber": 456
}
]
}
在我的组件中,我订阅了服务以填充 customerData。
private _getData() {
this._myService
.getData()
.subscribe(
customerData => this.customerData = customerData,
error => this.errorMessage = error
);
}
此代码工作正常。
在 JSON 转储中有两个数据“分组”,客户数据和产品数据,后者在数组中。理想情况下,我想单独填充产品数据。像这样的:
.subscribe(
customerData => this.customerData = customerData,
productData => this.productData = customerData.products
error => this.errorMessage = error
);
这可能吗?如果可以,我将如何编写订阅代码?
【问题讨论】: