kolhapuri
kolhapuri

Reputation: 1651

Typescript : AngularJS retrieving a result object

I have a AngularJS service in Typescript that makes a get https call returns some data. My results is a child object of the response.data as follows response.data.Result.

How do I get access to it? I cannot do response.data.Result as I get compile time error where the response.data defined under IHttpPromiseCallbackArg<T> does not have the result property.

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

Upvotes: 2

Views: 1217

Answers (1)

W&#233;dney Yuri
W&#233;dney Yuri

Reputation: 1277

After this.$http.get(this.webApiUrl + '/api/applications'), try changing from ng.IHttpPromiseCallbackArg<IApplication[]> to { data: { Result: IApplication[] } } type.

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

Upvotes: 4

Related Questions