很明显,我没有从其他JS文件之一正确传递变量。
据我了解,如果我注入文件,则可以从该文件中调用函数。我能够在名为businesssService.uploadImageToFireBase的businesss.controller中完成此操作,但是我没有收到该变量,或者当我尝试执行该操作时它不可读,我相信这与来自business.service.js和firebase-stor的过程相同.service.js。
我收到错误:
angular.js:13424 ReferenceError: storageRef is not defined
businesses.service.js:
(function() {
'use strict';
angular
.module('bizdir-frb-admin.businesses')
.factory('businessesService', businessesService);
businessesService.$inject = ['firebaseDb', '_', '$firebaseArray', '$firebaseObject', 'listsService', 'firebaseStor'];
/* @ngInject */
function businessesService(firebaseDb, _, $firebaseArray, $firebaseObject, listsService,firebaseStor) {
var collectionName = 'businesses';
var query = firebaseDb.child(collectionName);
var businesses = $firebaseArray(query);
var service = {
selectAll: selectAll,
selectOne: selectOne,
saveItem: saveItem,
deleteItem: deleteItem,
insert: insert,
uploadImageToFireBase: uploadImageToFireBase
};
return service;
function uploadImageToFireBase(uploadedFile) {
// File or Blob named mountains.jpg
var file = uploadedFile;
//var storageRef = firebaseStor.firebaseStor();
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
}, function() {
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
});
}
firebase-stor.service.js:
(function() {
'use strict';
angular
.module('bizdir-frb-admin.infrastructure')
.factory('firebaseStor', firebaseStor);
firebaseStor.$inject = ['ENV', 'firebaseDb', 'fireDataService'];
/* @ngInject */
function firebaseStor(ENV) {
//firebase.initializeApp(ENV.firebaseConfig2);
var storage = firebase.storage();
var storageRef = storage.ref();
return storageRef;
}
//
})();
business.controller.js:
(function() {
'use strict';
angular
.module('bizdir-frb-admin.businesses')
.controller('BusinessController', BusinessController);
BusinessController.$inject = [
'businessesService', '$stateParams', 'cameraService', 'amazonS3Service', '$ionicLoading', '$window', '$state', 'fieldEditor', 'listsService', '$ionicPopup', '_', 'ENV','firebaseStor'];
/* @ngInject */
function BusinessController(
businessesService, $stateParams, cameraService, uploadToFireBase, amazonS3Service, $ionicLoading, $window, $state, fieldEditor, listsService, $ionicPopup, _, ENV, firebaseStor) {
var vm = angular.extend(this, {
columnWidth: Math.ceil(100 / ENV.columnsInGallery),
item: null,
imageGroups: [],
uploadImage: uploadImage,
uploadImageWebView: uploadImageWebView,
uploadToFireBase: uploadToFireBase,
removeImage: removeImage,
isWebView: !$window.Camera,
chooseCategory: chooseCategory,
chooseAnnotations: chooseAnnotations,
chooseTags: chooseTags,
changeDescription: changeDescription,
showOpenHours: showOpenHours,
valueChanged: valueChanged,
validateOnEmptyString: validateOnEmptyString,
editCatalogs: editCatalogs,
editProducts: editProducts,
editNews: editNews,
editServices: editServices
});
function uploadToFireBase(uploadedFile) {
businessesService.uploadImageToFireBase(uploadedFile);
}