Service showing undefined in angular

I have wrote one service which will upload image to server and I am using that service from controller but on call to service showing that undefined function which is defined in service. and error is something like this "Cannot read property 'uploadFileToUrl' of undefined"

here is my code

app.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file,clientid, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        fd.append('id', clientid);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(response){
            console.log(response)
        })
        .error(function(){
        });
    }
}]);

and here i am calling this service

app.controller('settingsCtrl', ['$scope','apiCall','$filter', function($scope,apiCall,$filter,fileUpload){
    $scope.saveStudioBranch = function(){
        studio();
        admin();
        branch();
    }

    function studio(){
        var studio_json = {"session_id":session_id,"u":[{"col":"studio","val":$scope.studioDetails.studio},{"col":"type","val":$scope.studioDetails.type}],"filter":[["id","=","1"]],"table":"studio"};
        apiCall.callEndpoint(studio_json,"settingRoute.php","update",json_header).then(function(response){
            if(response.data.error == 0){
                if($scope.inst_logo != undefined ){
                    var uploadUrl = "https://papa.fit/routes/registrationRoute.php?action=instLogo";
                    -->> fileUpload.uploadFileToUrl($scope.inst_logo,session.client_id,uploadUrl);
                }
            }
            else
                show_snack(response.data.message);
        });

    }
});

I have added a arrow where I am calling service from


the error is in the injection

app.controller('settingsCtrl', ['$scope','apiCall','$filter', 
  function($scope, apiCall, $filter, fileUpload){

you forgot to add fileUpload service inside the list of parameters

app.controller('settingsCtrl', ['$scope','apiCall','$filter', 'fileUpload',
  function($scope, apiCall, $filter, fileUpload){

您必须退还您的服务,否则将不确定

  function uploadFileToUrlSrv($http) {

    var service = {};
    service.uploadFileToUrl = function(file,clientid, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        fd.append('id', clientid);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(response){
            console.log(response)
        })
        .error(function(){
        });
    }
    return service;
}
链接地址: http://www.djcxy.com/p/77994.html

上一篇: 为什么用'新'创建Angular.js服务?

下一篇: 显示角度未定义的服务