I'm just a beginner at AngularJS. I'm trying to construct a method call on my Angular data service, to which I pass an object that needs to be posted to the server. The Name of the data service is requestsRepository and the name of the controller is EnterNewRequestCtrl. My service has a method "postServiceRequest", to which I'm trying to pass my data object to be posted. When I call this methon from the controller, I get the following error: TypeError: Cannot call method 'postServiceRequest' of undefined at Scope.$scope.submitRequest (http://myhost.mydomain.com:9221/controllers/EnterNewRequestCtrl.js:9:28)
Here is a complete listing of both the service and the controller.
The service:
frontEndApp.factory('requestsRepository', function ($http) {
var postServiceRequest = function (ServiceRequest) {
$http({
url: 'http://localhost:8080/api/Customers',
method: "POST",
data: ServiceRequest,
headers: { 'Content-Type': 'application/json' }
}).success(function (data, status, headers, config) {
//$scope.persons = data; // assign $scope.persons here as promise is resolved here
console.log("postServiceRequest SUCCESS");
}).error(function (data, status, headers, config) {
//$scope.status = status;
console.log("postServiceRequest FAILURE: " + status);
});
}
});
And now the controller:
angular.module('frontEndApp').controller('EnterNewRequestCtrl',function ($scope, requestsRepository) {
$scope.RequestorName;
$scope.RequestorBusinessUnit;
$scope.submitRequest = function () {
var ServiceRequest = {
requestorName: $scope.RequestorName,
requestorBusinessUnit: $scope.RequestorBusinessUnit
};
requestsRepository.postServiceRequest(ServiceRequest);
}
});
What is the proper way of implementing this controller - server method call?