使用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 );
这是可能的,如果是这样,我将如何编码订阅?
解决方法
你可以写订阅
subscribe( customerData => { this.customerData = customerData; this.productData =customerData.products; },error => this.errorMessage = error );
要么
subscribe( function(customerData) { this.customerData = customerData; this.productData =customerData.products; },error => this.errorMessage = error );