2017-06-19 21 views
0

私はAngularを初めて使ったので、(クラス外の)関数に.mapを使ってWebService(REST)を呼び出しました。私は自動的にオブジェクト "応答"を取得します。しかし今、私のメソッド(と私のクラス)の中にある値をクラスの外にある関数にどのように送ることができますか?この値はWebServiceの応答とは関係ありません。「クラス」から「関数」に別の値を渡す方法

@Injectable() 
export class MyClass { 

    getEvents(myValue: string): Observable<CalendarEvent[]> { 

    this.http 
     .get("url of webservice") 
     .map(mapEvents) <- how can I put "myValue" here 
     .catch(handleError); 
} 
} 

function mapEvents(response: Response): CalendarEvent[] { 
    return response.json().map(toEvent); 
} 

function toEvent(event: any): CalendarEvent { 
    const s_event = <CalendarEvent>(
    { 
     value : myValue; <- to get "myValue" here? 
    }); 
    return s_event; 
} 
+1

あなたがすることはできませんを使用し、それはそこに利用できません。明示的に渡す必要があります(例: '.map(event => mapEvents(event、myValue))'です。 – jonrsharpe

+0

質問が不明です – Ced

答えて

1

はちょうどarrow function

@Injectable() 
export class MyClass { 

    getEvents(myValue: string): Observable<CalendarEvent[]> { 

    this.http 
     .get("url of webservice") 
     .map(d => mapEvents(d, myValue)) <- put "myValue" here 
     .catch(handleError); 
} 
} 

function mapEvents(response: Response, myValue): CalendarEvent[] { 
    return response.json().map(e => toEvent(e, myValue)); 
} 

function toEvent(event: any, myValue): CalendarEvent { 
    const s_event = <CalendarEvent>(
    { 
     value : myValue; <- to get "myValue" her ! 
    }); 
    return s_event; 
} 
関連する問題