Javascript 服务显示角度未定义
我已经写了一个服务,将上传图像到服务器,我使用的是来自控制器的服务,但在调用服务时显示了服务中定义的未定义的功能。错误是这样的“无法读取未定义的属性'uploadFileToUrl'” 这是我的代码Javascript 服务显示角度未定义,javascript,angularjs,angular-services,Javascript,Angularjs,Angular Services,我已经写了一个服务,将上传图像到服务器,我使用的是来自控制器的服务,但在调用服务时显示了服务中定义的未定义的功能。错误是这样的“无法读取未定义的属性'uploadFileToUrl'” 这是我的代码 app.service('fileUpload', ['$http', function ($http) { this.uploadFileToUrl = function(file,clientid, uploadUrl){ var fd = new FormData();
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.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);
});
}
});
我在呼叫服务的地方添加了一个箭头,您必须返回您的服务,否则它将是未定义的
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;
}
错误在注入过程中
app.controller('settingsCtrl', ['$scope','apiCall','$filter',
function($scope, apiCall, $filter, fileUpload){
您忘记在参数列表中添加fileUpload
服务
app.controller('settingsCtrl', ['$scope','apiCall','$filter', 'fileUpload',
function($scope, apiCall, $filter, fileUpload){
我不明白你的意思。你能简单解释一下吗?@sameerdighe服务是一组功能。您需要将服务作为对象返回才能在控制器中使用它我已经用代码示例编辑了您的服务应该是什么样子,但请记住,创建服务时必须始终返回服务:)@GuillaumeRoche Bayard与服务声明相关的操作代码是正确的。阅读并显示代码,你在哪里注入服务哦,对不起。我将编辑以上代码,请重新检查