let headers = new Headers({ 'Authorization': `${this.user.Token}` });
let options = new RequestOptions({ headers: headers });
this.http.get(environment.apiRoot + 'Customers/Customer/Search/' + term + '/' + value, options)
.map((response) => { // This extracts the data you require
var Data = response.json().Data;
return Data;
}).switchMap(Data => { // This returns the result of the second observable in
return this.http.request(environment.apiRoot + 'Orders/Order/Customer/' + Data.ID, options)
.map(odResponse => { // We have the response. Extract the json, and return an array with the first element as the old data, and the second element as the new data.
var _Data = odResponse.json().Data;
return [ Data, _Data ];
});
}).subscribe( data => {
let firstSet = data[0];
let secondSet = data[1];
// Now do your assignment
this.data.customer.emailAddress = firstSet.EmailAddress;
this.data.customer.phoneNumber = firstSet.PhoneNumber;
...
...
});
我使用这种方法是因为似乎需要第一次调用返回的数据(ID)才能进行第二次调用。如果不是这样,我会建议使用Observable.forkJoin。
This
是RxJS V4中forkJoin的文档,但它仍然适用于V5。