如何在RxJS subscribe()函数中获取变量?

时间:2017-02-23 05:02:08

标签: angular typescript rxjs

我正在使用angular2。 现在,我很难在subscribe()中获取一个值。 我知道它会返回null。 如何返回正确的值?

这是我的代码。任何人都给我建议,这将是非常有帮助的。谢谢:))



 deliveryCancel(branchId, deliveryId) {
    const headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.put(this.testUrl + '/admin/branches/' + branchId + '/deliveries/' + deliveryId + '/cancel', {
      headers: headers
    })
      .catch(handleError);
  }




deliveryCancel()



#include <iostream>
#include<math.h>

using namespace std;

int main()
{
    int num, swno;
    int fd, ld, digits;

    // Reads a number from user
    cout<<"Enter any number:";
    cin>>num;

    ld = num % 10; //Gets last digit
    digits = (int)log10(num); //Total number of digits - 1
    fd = (int)(num / pow(10, digits)); //Gets the first digit

    swno = ld;
    swno *= (int)pow(10, digits);
    swno += num % ((int)pow(10, digits));
    swno -= ld;
    swno += fd;

    cout<<"\nOriginal number = "<<num;
    cout<<"\nNumber after swapping first and last digit: "<<swno;

    return 0;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您无法像这样访问res.json()。并初始化数据&#39;作为类变量。试试这个。

data = {};

this.deliveryService.deliveryCancel(rowInfo.branchId, rowInfo.id).subscribe(
          res => {
            this.data = res['_body'];
            this.getDeliveryList();
            this.msgs.push({ severity: 'success', summary: 'Info', detail: rowInfo.deliveryStatus });
          },
          err => {
            this.errMessage = <any>err;
            this.msgs.push({ severity: 'error', summary: 'Error', detail: this.errMessage });
          }
        );

        console.log(data);