2017-10-10 129 views
2

我已經編寫了一個將圖像上傳到服務器的服務,我正在使用來自控制器的服務,但是調用服務時顯示服務中定義的未定義函數。和錯誤是這樣的「的未定義無法讀取屬性 'uploadFileToUrl'」角度未定義的服務

這裏是我的代碼

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(){ 
     }); 
    } 
}]); 

我在這裏調用這個服務

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); 
     }); 

    } 
}); 

我添加了一個箭頭,我打電話給服務

+0

顯示代碼你在哪裏注入服務 – Satpal

+0

ohh對不起。我會編輯上面的代碼,並請重新檢查它 –

回答

4

錯誤是在注射

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

你忘了添加的參數列表裏面fileUpload服務

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

哦,我的壞。非常感謝你卡里姆 –

+0

不客氣:) – Karim

0

你必須返回你的服務,否則這將是不確定的

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; 
} 
+0

我沒有得到你。你能簡要介紹一下嗎? –

+0

@sameerdighe服務是一組功能。您需要將您的服務作爲對象返回,以便在您的控制器中使用它 – Zooly

+0

我已使用代碼示例編輯了您的服務應該是什麼樣子,但是請注意,您必須**始終在您返回服務時創建它:) –