繁体   English   中英

TypeError:Service.functionName不是函数

[英]TypeError: Service.functionName is not a function

我有这样的服务

angular.module('services').factory('DownloadService',[$resource,
  function($resource){
    return $resource('path/to/service/download', {}, {
      downloadFile: {
             method: 'GET',
             url: 'path/to/service/download?fileID=:fileID'
      }
    });
  }
]);

然后在控制器中,我有一个函数,我在其中调用此服务并在单击文件时调用该函数

    angular.module('search').controller('MainCtrl', ['$scope','DownloadService',
      function ($scope,DownloadService){

        $scope.downloadDocument = function(id){
           DownloadService.downloadFile({
            fileId: id
           });
        };
}])

我收到此错误

TypeError:DownloadService.downloadFile is not a function...

你有错字

nw loadFile

DownloadService.download FI

确保在创建search模块时已将services模块指定为依赖项,例如

angular.module('search', ['services', ...])

此外,请确保您在search或它依赖的其他任何模块中都没有其他服务(名称为DownloadService


您可能只想在服务的DownloadService中添加alertconsole.log ,以确保已实例化该alertconsole.log

或更好的办法是,在两个地方都将其重命名为唯一的名称(例如myDownloadService )。

console.log(new DownloadService())并检查对象的$downloadFile也是确保angular正在注入正确服务的另一种方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM