我在Typescript中有一个AngularJS服务,它使get https调用返回一些数据。
我的结果是response.data
的子对象,如下所示response.data.Result
。
我如何获得它?我无法执行response.data.Result
,因为在response.data
下定义的IHttpPromiseCallbackArg<T>
没有结果属性时出现编译时错误。
class ApplicationService implements IApplicationService {
private webApiUrl;
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) {
this.webApiUrl = 'http://localhost';
this.$http = $http;
}
getApplications(): ng.IPromise<IApplication[]> {
return this.$http.get(this.webApiUrl + '/api/applications')
.then((response: ng.IHttpPromiseCallbackArg<IApplication[]>): IApplication[]=> {
return <IApplication[]>response.data;
});
}
}
答案 0 :(得分:4)
在this.$http.get(this.webApiUrl + '/api/applications')
之后,尝试从ng.IHttpPromiseCallbackArg<IApplication[]>
更改为{ data: { Result: IApplication[] } }
类型。
class ApplicationService implements IApplicationService {
private webApiUrl;
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) {
this.webApiUrl = 'http://localhost';
this.$http = $http;
}
getApplications(): ng.IPromise<IApplication[]> {
return this.$http.get(this.webApiUrl + '/api/applications')
.then((response: { data: { Result: IApplication[] } }): IApplication[]=> {
return response.data.Result;
});
}
}