【问题标题】:Then is not a function error in angularjs然后不是angularjs中的函数错误
【发布时间】:2017-12-03 05:04:42
【问题描述】:

我有一个返回员工数据的简单函数。但是获取 then 不是函数错误如何从函数中检索响应

   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;

【问题讨论】:

  • 显示angularService.GetData方法
  • 假设angularService.GetData(...args) 返回Promise。您应该使用$scope.searchMethod.then(function ... ) 而不是$scope.searchMethod().then(function ... )
  • 你能发布你的角度服务的完整代码吗?

标签: angularjs


【解决方案1】:

应该在 searchMethod 而不是 searchMethod() 上调用 .then - 假设 getdata 返回一个承诺,因为您不能在不返回承诺的函数上调用 .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);

        });
    }

【讨论】:

  • 如果我通过标准 getData 它让我为空。这没有返回任何响应
  • 如果你没有通过标准,会发生什么?
  • 如果我没有通过,那么我的 web api 中的标准不为空,它有数据。
  • 是否使用 $scope.searchMethod.then 而不是 $scope.searchMethod().then 解决问题?
  • 您现在遇到什么错误?你的 angularService.GetData 函数是什么样的?
【解决方案2】:

正如@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);}

【讨论】:

  • 即使服务名称相同仍然失败
  • 你能发布你的 apiService 吗?
  • 我使用了$scope.searchMethod.then(function (response) 而不是$scope.searchMethod().then(function (response) 仍然失败
  • 你用的东西太多了。这条线做了什么 apiService.DeferredPost(url, criteria) ?
  • 贴出api服务方式
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 2013-10-13
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2015-04-22
相关资源
最近更新 更多