我的$http调用看起来像这样,我想知道最灵活的方法来处理.success和.error中返回的所有参数?
this.$http({ url: "/api/x,method: "GET" }) .success((??) : void => { }) .error((??) : void => { })
Angular文档告诉我返回以下内容:
data – {string|Object} – The response body transformed with the transform functions. status – {number} – HTTP status code of the response. headers – {function([headerName])} – Header getter function. config – {Object} – The configuration object that was used to generate the request. statusText – {string} – HTTP status text of the response.
angular.d.ts告诉我:
interface IHttpPromiseCallback<T> { (data: T,status: number,headers: (headerName: string) => string,config: IRequestConfig): void; } interface IHttpPromiseCallbackArg<T> { data?: T; status?: number; headers?: (headerName: string) => string; config?: IRequestConfig; statusText?: string; }
但我仍然感到困惑.有没有人使用上面的两个接口来定义回调,他们是如何做到的?
理想情况下,我希望有这样的东西:
.success((data: any,config: any) : void => {
但是使用接口.
任何人都可以告诉我,如果我在正确的轨道上,如果我可以使用接口,而不是必须在参数后指定:any:number等.
以下是GET的示例:
原文链接:https://www.f2er.com/angularjs/142240.htmlprivate getInternal<T>(url) { var deferred = this.$q.defer(); this.$http({ url: url,method: "GET" }) .success((data: T,config: ng.IRequestConfig): void => { if (status == 200 && headers('location') == null && config.timeout > 200) { //do something with data } return deferred.resolve(data); }) .error((data: T,config: ng.IRequestConfig): void => { if (status == 500 && headers('myAuth') != null && config.method == 'GET') { // write to log } return deferred.reject(data); }); }
现在让我们说你有一个服务,你知道你得到一个特定的对象:
this.getInternal<mySpecObject>('service/abc').then(res => doSomethingWithData(res));