服务未在控制器中引发错误,因为函数未定义

时间:2019-03-22 06:53:13

标签: angularjs

amcApp.service('utilService', function ($http, $q,$rootScope) {
    var getSupportTypes = function () {

        var deferred = $q.defer();
        $http.get($rootScope.BaseUrl+'vendor/supportTypes')
            .then(function daoSuccess(response) {
                console.log("Getting Sub Customer Service call success ", response);
                deferred.resolve(response);
            }, function daoError(reason) {
                console.log("Getting SubC data service call error", reason);
                deferred.reject(reason);
            });
        return deferred.promise;
    };
    return{
        getSupportTypes : getSupportTypes,       
    };
});

以上是我定义的服务。

下面是我定义的控制器。

amcApp.controller('contractForm', ['$scope', '$http','$rootScope','$filter', '$uibModal', '$state', 'testService','utilService','contractService',
 function ($scope, $http,$rootScope, $filter,$uibModal, testService,utilService,contractService) {
          //Service of getting the Support Types.
        utilService.getSupportTypes().then(function(response){
                        $scope.supportTypes = response.data.UtilDataType;
        });
}]);

这是我遇到的错误 enter image description here

我能得到任何建议吗?

1 个答案:

答案 0 :(得分:0)

检查您注入控制器的依赖项,数组中有一个额外的$ state,尚未传递给该函数。我建议删除以下内容:

amcApp.controller('contractForm', ['$scope', '$http', '$rootScope', '$filter', '$uibModal', 'testService','utilService','contractService',
 function ($scope, $http, $rootScope, $filter, $uibModal, testService, utilService, contractService) {
   //Service of getting the Support Types.
   utilService.getSupportTypes().then(function (response){
      $scope.supportTypes = response.data.UtilDataType;
   });
}]);

另一方面,我会删除所有未使用的服务,我想这也可以:

amcApp.controller('contractForm', ['$scope', 'utilService', function ($scope, utilService) {
   //Service of getting the Support Types.
   utilService.getSupportTypes().then(function (response) {
      $scope.supportTypes = response.data.UtilDataType;
   });
}]);

仅是一个建议-如果保持代码整洁,找出任何问题的根本原因就容易得多。