2016-09-08 19 views
0

このangular2サービスがapp/heroes/{{country}}のようなベースに、その場でheroesUrl動的パラメータを渡すと、私はそれを行うだろうかObservablesと動的URLパラメータを含むAngular2 http getリクエスト。の仕方?

getHeroes(country) {}

import { Injectable }  from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Hero }   from './hero'; 
import { Observable }  from 'rxjs/Observable'; 
@Injectable() 
export class HeroService { 
    constructor (private http: Http) {} 
    private heroesUrl = 'app/heroes'; // URL to web API 
    getHeroes(): Observable<Hero[]> { 
    return this.http.get(this.heroesUrl) 
        .map(this.extractData) 
        .catch(this.handleError); 
    } 
    private extractData(res: Response) { 
    let body = res.json(); 
    return body.data || { }; 
    } 
    private handleError (error: any) { 
    // In a real world app, we might use a remote logging infrastructure 
    // We'd also dig deeper into the error to get a better message 
    let errMsg = (error.message) ? error.message : 
     error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
    console.error(errMsg); // log to console instead 
    return Observable.throw(errMsg); 
    } 
} 

のようにそれを使用することができますし、変更しようとすると、観察可能と公式ドキュメントから取られました?

答えて

1

私は試してみるだろう

getHeroes(country) {} 


export class HeroService { 
    constructor (private http: Http) {} 
    private heroesUrl = 'app/heroes'; // URL to web API 
    getHeroes (country): Observable<Hero[]> {    //<-----added parameter 
    return this.http.get(this.heroesUrl + '/' + country) //<-----changed 
        .map(this.extractData) 
        .catch(this.handleError); 
    } 
    ... 
    ... 
} 
+0

おかげで、私はあなたのポイントを理解している場合、あなたは物事を次の操作を行う必要があると思います – deroccha

関連する問題