Reputation: 979
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
Upvotes: 0
Views: 703
Reputation: 8269
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()
}
Upvotes: 1