代码之家  ›  专栏  ›  技术社区  ›  Nikhil Radadiya

Angular2 http请求取决于从其他响应返回的数据

  •  1
  • Nikhil Radadiya  · 技术社区  · 8 年前

    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

    组成部分输电系统

    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()

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sravan    8 年前

    在你的 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; 
        }    
      },
      )
    }