2016-09-08 3 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