Angular 从rest调用返回数据
在一个组件中,我有以下内容:Angular 从rest调用返回数据,angular,angular2-components,Angular,Angular2 Components,在一个组件中,我有以下内容: ngOnInit() { this.route.params.subscribe(params => { this.localEventEdit = this.getLocalEvent(+params['id']) console.log(this.localEventEdit) }); } getLocalEvent(localEventId: number) { this.restCall.ge
ngOnInit() {
this.route.params.subscribe(params => {
this.localEventEdit = this.getLocalEvent(+params['id'])
console.log(this.localEventEdit)
});
}
getLocalEvent(localEventId: number) {
this.restCall.get("/localevents/" + localEventId, (data) => {
this.localEventEdit = data;
});
return {name: "asdasd", languageId: 1, locationId: 1};
}
我想将数据从getLocalEvent
中的restCall返回到ngOnInit
中的this.localEventEdit
变量
这是restCall.get
:
//HTTP GET Request
//Default return type is JSON
public get(path: string, callback: (data) => void, returnType: number = RestCall.RETURN_TYPE_JSON) {
this.auth.retrieveToken(path).subscribe(
tokenResponse => {
this.http.get(this.location + path, this.getRequestOptions(returnType))
.map((res: Response) => {
return this.handleResponse(res, returnType);
}).subscribe(
data => callback(data),
error => this.handleError(error)
)
},
tokenError => this.handleError(tokenError)
);
}
有什么想法吗?此时,我只能返回
return{name:“asdasd”,languageId:1,locationId:1}
但是我想从restcall返回数据。不能以同步方式返回异步值。建议使用Observable和async管道,如下所示:
ngOnInit() {
this.route.params.subscribe(params => {
this.localEventEdit = this.getLocalEvent(+params['id'])
console.log(this.localEventEdit)
});
}
getLocalEvent(localEventId: number) {
this.restCall.get("/localevents/" + localEventId, (data) => {
this.localEventEdit = data;
});
return {name: "asdasd", languageId: 1, locationId: 1};
}
组件。ts
localEventEdit$: Observable<any>;
ngOnInit() {
const id = this.route.snapshot.params['id'];
this.localEventEdit$ = this.getLocalEvent(+id);
}
getLocalEvent(localEventId: number): Observable<any> {
return this.restCall.get("/localevents/" + localEventId);
}
get(path: string): Observable<any> {
const url = this.location + path;
return this.auth.retrieveToken(path) // is it right that you don't use the token???
.switchMap(tokenResponse => this.http.get(url, this.getRequestOptions(returnType))
.map((res: Response) => this.handleResponse(res, returnType));
}
localEventEdit$:可观察;
恩戈尼尼特(){
const id=this.route.snapshot.params['id'];
this.localEventEdit$=this.getLocalEvent(+id);
}
getLocalEvent(localEventId:number):可观察{
返回此.restCall.get(“/localevents/”+localEventId);
}
服务.ts
localEventEdit$: Observable<any>;
ngOnInit() {
const id = this.route.snapshot.params['id'];
this.localEventEdit$ = this.getLocalEvent(+id);
}
getLocalEvent(localEventId: number): Observable<any> {
return this.restCall.get("/localevents/" + localEventId);
}
get(path: string): Observable<any> {
const url = this.location + path;
return this.auth.retrieveToken(path) // is it right that you don't use the token???
.switchMap(tokenResponse => this.http.get(url, this.getRequestOptions(returnType))
.map((res: Response) => this.handleResponse(res, returnType));
}
get(路径:字符串):可观察{
const url=this.location+path;
返回this.auth.retrieveToken(path)//不使用令牌对吗???
.switchMap(tokenResponse=>this.http.get(url,this.getRequestOptions(returnType))
.map((res:Response)=>this.handleResponse(res,returnType));
}
component.html
<div>{{ localEventEdit$ | async | json }}</div>
{{localEventEdit$| async | json}
异步管道将自动为您管理订阅