2016-08-10 48 views
0

我一直在嘗試創建一個可以從控制器調用的工廠。 但我也無法。打字稿工廠與angularjs

我patientview.html:

<pThis is the patientsearch view.</p 
<button type="button" class="btn btn-large btn-block btn-default" ng-click="click()">button</button> 

我patientviewController:

module tsRisApp { 
export interface IPatientregistrationScope extends ng.IScope { 
click:any; 
data: any[]; 
} 
export class PatientviewCtrl { 
constructor (private $scope: IPatientregistrationScope) { 
    $scope.click = function(){ 
    let data = patientFactory().SearchPatients("Doe"); 
    } 
}  
} 
} 

angular.module('tsRisApp') 
    .controller('PatientViewCtrl', tsRisApp.PatientViewCtrl); 

這是我patientFactory:

'use strict'; 

module tsRisApp { 
export function patientFactory() { 
    return new Patientfactory(); 
} 


export class Patientfactory { 
static $inject = ['$http', '$location']; 
http: ng.IHttpService; 
location: ng.ILocationService; 

constructor() { 
    } 

SearchPatients(searchString:string) { 
    let request = new Request(); 
    request.Compression = "no" 
    request.ServiceType = "SearchPatients" 
    request.SessionId = "SessionLessRequest" 
    request.Parameters = searchString; 
    let jsonRequest = JSON.stringify(request); 
    this.http.post("http://" +this.location +"request.php/", jsonRequest).then(function (response) { 
    return response.data; 
    }) 

} 
} 
} 

當點擊按鈕,我得到以下錯誤:

angular.js:13920 TypeError:無法讀取未定義的屬性'post'

我錯過了什麼? 感謝您的幫助

回答

1

我相信,在你的構造函數,它應該是:

export class Patientfactory { 

static $inject = ['$http', '$location']; 

constructor (private http: ng.IHttpService, 
      private location: ng.ILocationService) {} 

SearchPatients(searchString: string) { 

    //Then you can access your http.post 
    this.$http.post() 

}