我是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;
}
答案 0 :(得分: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;
}