2017-06-19 97 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; 
} 
相關問題