我只是AngularJS的初学者。我正在尝试在我的Angular数据服务上构造一个方法调用,我传递了一个需要发布到服务器的对象。数据服务的名称是requestsRepository,控制器的名称是EnterNewRequestCtrl。我的服务有一个方法“postServiceRequest”,我正试图传递我要发布的数据对象。当我从控制器调用这个方法时,我得到以下错误:TypeError:无法调用undefined方法'postServiceRequest' 在Scope。$ scope.submitRequest(http://myhost.mydomain.com:9221/controllers/EnterNewRequestCtrl.js:9:28)
以下是服务和控制器的完整列表。
服务:
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);
});
}
});
现在是控制器:
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);
}
});
实现此控制器的正确方法是什么 - 服务器方法调用?
答案 0 :(得分:2)
您需要使用postServiceRequest()
方法返回一个对象...
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);
});
};
return { postServiceRequest : postServiceRequest };
});
- 编辑 -
一旦你开始工作,你也应该改变postServiceRequest()以从$ http()调用返回promise,然后在你的控制器中使用该promise来填充$ scope的变量...
var frontEndApp = angular.module('frontEndApp', []);
frontEndApp.factory('requestsRepository', function ($http) {
var postServiceRequest = function (ServiceRequest) {
return $http({
url: 'http://localhost:8080/api/Customers',
method: "POST",
data: ServiceRequest,
headers: { 'Content-Type': 'application/json' }
});
};
return { postServiceRequest : postServiceRequest };
});
frontEndApp.controller('EnterNewRequestCtrl',function ($scope, requestsRepository) {
$scope.RequestorName;
$scope.RequestorBusinessUnit;
$scope.submitRequest = function () {
var ServiceRequest = {
requestorName: $scope.RequestorName,
requestorBusinessUnit: $scope.RequestorBusinessUnit
};
requestsRepository.postServiceRequest(ServiceRequest)
.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);
});
};
});