typescript Angular http post 通过服务将数据返回给组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46170084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular http post to return data back through service to component
提问by
I Want to return data (@@identity) from sql server through the service to the component
我想通过服务从sql server返回数据(@@identity)给组件
The Web api is definitely being called and sending data back.
肯定会调用 Web api 并将数据发回。
However, since I am new to Angular 2/4 ( i was used to angular 1 ) i would normally just do return
on a service , and set a variable or object to the caller.
但是,由于我是 Angular 2/4 的新手(我习惯于 angular 1),我通常只会return
在服务上做 ,然后为调用者设置一个变量或对象。
Currently this is what I am doing
目前这就是我正在做的
Component
零件
this.trackerFormService.addSession(); // CURRENT
but since the value is a single id, like 5, shouldn't i create something in typescript to retrieve it?
但由于该值是一个单一的 id,比如 5,我不应该在打字稿中创建一些东西来检索它吗?
this.myId = this.trackerFormService.addSession(); // ???
Then in the Service
然后在服务
private _url = 'http://localhost:60696/api/tracker';
addSession() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(this._url, JSON.stringify({}), options).catch(this.handleError).subscribe();
}
Now for the above to pass the ID from the service (addSession() method back to component, shouldn't i do a return
?
现在为了将 ID 从服务(addSession() 方法传回组件),我不应该做一个return
吗?
return this.http.post(this._url, JSON.stringify({}), options).catch(this.handleError).subscribe();
but will that truly even work?
但这真的有效吗?
Web api (.net core ) looks like this
Web api (.net core ) 看起来像这样
return Created($"api/sessiontrackers/{sessionTracker.Id}", sessionTracker);
采纳答案by Dheeraj Kumar
This will surely help you with making post call to web api
这肯定会帮助您对 web api 进行后期调用
In your service
为您服务
return this.http.post(Url, JSON.stringify(data), requestOptions)
.map((response: Response) => response.json())
In your Component
在您的组件中
this.service.addSession(this.data).subscribe(
data => { console.log(data) // Data which is returned by call
},
error => { console.log(error); // Error if any
},
()=> // Here call is completed. If you wish to do something
// after call is completed(since this is an asynchronous call), this is the right place to do. ex: call another function
)
回答by Suren Srapyan
You can return the Observable
from your addSession
, without .subscribe
您可以Observable
从您的addSession
,返回.subscribe
addSession() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this._url, JSON.stringify({}), options).catch(this.handleError);
}
And because this is an asynchronous call, subscribe
to your observable in your component and when it will finish the call, assign the value to your myId
.
并且因为这是一个异步调用,所以subscribe
对组件中的 observable 以及当它完成调用时,将值分配给您的myId
.
this.trackerFormService.addSession().subscribe(result => this.myId = result);