Angular2 http请求取决于从其他响应

时间:2017-09-05 11:38:04

标签: angular typescript angular-services

我创建了一个服务,它使用diff params进行不同的http调用。 的 quote.service.ts

getQuotes(){
    let params = {
        "Type": "BasicDetail",
    }
    return this.http.post(this.url,params)
    .map(res => res.json())
}  

getOptions(){
    let params = {
        "Type": "Hoteloption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}

getServiceWiseOptions(){
    let params = {
        "Type": "ServiceWiseOption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}

我在getOption()

中的组件代码中调用constructor下方

component.ts

getOption() {
 this.quoteService.getQuotes().mergeMap( quotes => {
  if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
    return this.quoteService.getServiceWiseOptions()
  }
  else{
    return this.quoteService.getOptions()
  }
})
 .subscribe(
  options => {
    this.optionsdata = options.resultData;             
    if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
      this.servicewiseData = options.resultData.ServiceWiseOption;
    }else{                
      this.servicewiseData = options.resultData.Hoteloption; 
    }    
  },
  )
}

如果我getServiceWiseOptions() getOptions() getOption() QuotType:,我需要根据getServiceWiseOptions()的回复从服务中致电getOptions()getOption()

上面的函数mergeMap()有时可以工作,但有时它不会调用这些函数。

请给我一些建议我该怎么办? 我认为它与SELECT * FROM daily_report RIGHT JOIN (calendar c,user u) ON user_id=u.id AND date=c.datefield WHERE c.datefield='2017-08-02' AND u.shall_create_every_day=1

的问题

1 个答案:

答案 0 :(得分:1)

getOption()函数中,您使用this表示您的回复,这使其成为控制器变量而不是response数据,

所以将this.quotes更改为quotes

getOption() {
 this.quoteService.getQuotes().mergeMap( quotes => {
    this.quotes = quotes; // this line if you want it to use anywhere else
  if(quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
    return this.quoteService.getServiceWiseOptions()
  }
  else{
    return this.quoteService.getOptions()
  }
})
 .subscribe(
  options => {
    this.optionsdata = options.resultData;             
    if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
      this.servicewiseData = options.resultData.ServiceWiseOption;
    }else{                
      this.servicewiseData = options.resultData.Hoteloption; 
    }    
  },
  )
}