![](/img/trans.png)
[英]Angular Service being called on ngOnInit showing 'undefined', not showing icons
[英]Service showing undefined in angular
我寫了一個服務,它將圖像上傳到服務器,我正在使用控制器中的服務,但是在調用服務時顯示了服務中定義的未定義函數。 這樣的錯誤是這樣的: “無法讀取未定義的屬性'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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.