我有简单的功能,可以返回员工数据。但是,如果从函数中检索响应
,则不是函数错误 function getdata(criteria) {
return angularService.GetData(criteria, $scope.year, $scope.selectedYearType.name);
}
在以下功能中调用
$scope.GetEmployeeData = function (criteria) {
$scope.searchMethod = getdata;
$scope.searchMethod().then(function (response) {----------> Error here
var totalEmployeeAmount = 0;
for (var i = 0; i < response.data.results.length; i++) {
var summaryData = response.data.results[i];
totalEmployeeAmount += (summaryData.totalEmployeeAmount);
}
return response
}, function (response) {
// This is to see if has any error
//console.log(response);
});
}
我的角色服务
function getData(criteria, year, yearType) {
var url = apiService.ApiUrl + "/Employees/EmployeeHistory/GetData/" + year + "/" + yearType;
return apiService.DeferredPost(url, criteria);}
延期发布方法
function deferredPost(url, params) {
var deferred = $q.defer();
$http.post(url, params)
.then(function (data) {
deferred.resolve(data);
}, function (resp) {
deferred.reject(resp);
}).catch(function (data) {
deferred.reject(data);
});
return deferred.promise;
}
API
var api = {
DeferredPost: deferredPost
};
return api;
答案 0 :(得分:0)
应该在searchMethod上调用.then而不是searchMethod() - 假设getdata返回一个promise,因为你不能在不返回promise的函数上调用.then。另外我猜你应该把标准作为参数传递给getdata:
$scope.GetEmployeeData = function (criteria) {
$scope.searchMethod = getdata;
$scope.searchMethod.then(function (response) {
var totalEmployeeAmount = 0;
for (var i = 0; i < response.data.results.length; i++) {
var summaryData = response.data.results[i];
totalEmployeeAmount += (summaryData.totalEmployeeAmount);
}
return response
}, function (response) {
// This is to see if has any error
//console.log(response);
});
}
答案 1 :(得分:0)
正如@William Hampshire所说,你应该使用$scope.searchMethod.then(function ... )
代替$scope.searchMethod().then(function ... )
$scope.GetEmployeeData = function (criteria) {
$scope.searchMethod = getdata(criteria);
$scope.searchMethod.then(function (response) {
var totalEmployeeAmount = 0;
for (var i = 0; i < response.data.results.length; i++) {
var summaryData = response.data.results[i];
totalEmployeeAmount += (summaryData.totalEmployeeAmount);
}
return response
}, function (response) {
// This is to see if has any error
//console.log(response);
});
}
您的代码仍然失败的原因是您的服务名称中有拼写错误。
function getdata(criteria) {
return angularService.GetData(criteria, $scope.year, $scope.selectedYearType.name);
}
应该是GetData
而不是getData
。
function GetData(criteria, year, yearType) {
var url = apiService.ApiUrl + "/Employees/EmployeeHistory/GetData/" + year + "/" + yearType;
return apiService.DeferredPost(url, criteria);}