简体   繁体   中英

typescript factory with angularjs

I have been trying to create a factory that can be called from a controller. But am unable too.

My patientview.html:

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

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

And this is my 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;
  })

}
}
}

When clicking the button I get the following error:

angular.js:13920 TypeError: Cannot read property 'post' of undefined

What am I missing? Thanks for your help

I believe on your constructor, it should be:

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()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM