与angularjs的打字稿工厂

时间:2016-08-10 13:35:07

标签: angularjs typescript

我一直在尝试创建一个可以从控制器调用的工厂。 但我也不能。

我的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);

这是我的耐心工厂:

'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:无法读取属性&#39; post&#39;未定义的

我错过了什么? 谢谢你的帮助

1 个答案:

答案 0 :(得分: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()

}