显示角度未定义的服务
我写了一个将图像上传到服务器的服务,我使用的是来自控制器的服务,但是调用服务时显示服务中定义的未定义函数。 和错误是这样的“无法读取属性'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);
});
}
});
我在服务中添加了一个箭头
错误在注射中
app.controller('settingsCtrl', ['$scope','apiCall','$filter',
function($scope, apiCall, $filter, fileUpload){
您忘记了在参数列表中添加fileUpload
服务
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/77993.html