Angular 2 - 您如何订阅多个变量?

eba*_*nin 4 angular2-services angular

使用Angular 2,我从服务接收JSON数据.像这样的东西:

{
    "customerName": "foo",
    "customerAddress": "123 Somewhere",
    "products": 
    [
        {
            "productName": "bar",
            "productNumber": 123 
        },
        {
            "productName": "baz",
            "productNumber": 456             
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在我的组件中,我订阅了服务以获取填充customerData.

private _getData() {
    this._myService
        .getData()
        .subscribe(
            customerData => this.customerData = customerData,
            error => this.errorMessage = error
        );
}
Run Code Online (Sandbox Code Playgroud)

此代码正常工作.

在JSON转储中,有两个数据"分组",即客户数据和产品数据,后者在数组中.理想情况下,我想分别填充产品数据.像这样的东西:

        .subscribe(
            customerData => this.customerData = customerData,
            productData => this.productData = customerData.products
            error => this.errorMessage = error
        );
Run Code Online (Sandbox Code Playgroud)

这是可能的,如果是这样,我将如何编码订阅?

Ant*_*nio 7

你可以写订阅

    subscribe(
        customerData => 
        { 
         this.customerData = customerData;
         this.productData =customerData.products;
        },
        error => this.errorMessage = error
    );
Run Code Online (Sandbox Code Playgroud)

要么

   subscribe(
        function(customerData)
        { 
         this.customerData = customerData;
         this.productData =customerData.products;
        },
        error => this.errorMessage = error
    );
Run Code Online (Sandbox Code Playgroud)