加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:54:09 所属栏目:安全 来源:网络整理
导读:使用Angular 2,我从服务接收 JSON数据.像这样的东西: { "customerName": "foo","customerAddress": "123 Somewhere","products": [ { "productName": "bar","productNumber": 123 },{ "productName": "baz","productNumber": 456 } ]} 在我的组件中,我订阅
使用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
    );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读