2017-02-08 60 views
0

我用下面的代碼get方法如何通過多個嬰兒車在get方法angular2

this.required = {id:this.CategoriesId,fields:"courses.fields(largeIcon,instructor)",     includes:"courses"}; 

     this.courseraService.getEachCategory ("categories", this.required) 
       .subscribe (
         response => this.Courses = response, 
         error => this.errorMessage = <any>error, 
        () => console.log(this.Courses) 

      ); 


     getEachCategory(model,data){ 

      this.url = this.heroesUrl + model; 
      console.log(this.url); 
       console.log(data); 
      var params = data; 

      return this.http.get(this.url + params) 
        .map (response => response.json()) 
        .do(data => console.log ('All categories: ' + JSON.stringify(data))) 
        .catch(this.handleError); 
     } 

我試圖通過參數作爲對象,但得到以下錯誤的URL

GET https://api.coursera.org/api/catalog.v1/categories[object%20Object] 

回答

0
import {Http, Response, RequestOptions, URLSearchParams} from "@angular/http"; 

getEachCategory(model,data){ 
    this.url = this.heroesUrl + model; 

    var args = new RequestOptions(); 
    args.search = new URLSearchParams(); 

    // you can also repeate for loop for data array 
    args.search.append('key1', data[0].toString()); 
    args.search.append('key2', data[1].toString()); 
    args.search.append('key3', data[2].toString()); 
    return this._http.get(this.url, args) 
    .map((res:Response) => res.json()).toPromise(); 
}