打字稿:AngularJS检索结果对象

时间:2015-05-30 19:14:02

标签: angularjs typescript

我在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;
            });
        }
    }

1 个答案:

答案 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;
        });
    }
}