2015-05-30 130 views
2

我在Typescript中有一個AngularJS服務,它使get https調用返回一些數據。 我的結果是response.data的子對象,如下所示response.data.ResultTypescript:AngularJS檢索結果對象

我該如何訪問它?我不能這樣做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

打印'response'。它顯示了什麼? – softvar

回答

3

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