我對Angular相對較新,無法找到如何將$ rootScope注入到像我的那樣組織的工廠函數的確切答案。所以,任何幫助表示讚賞。基本上,我只是試圖注入$ rootScope,因爲我想在函數上傳(文件)中使用它。 。 。 。感謝皮特
這裏是我廠(一些代碼略去了):
(function() {
'use strict';
angular
.module('ooApp.controllers')
.factory('fileManager', fileManager);
fileManager.$inject = ['$q', 'fileManagerClient', 'appInfo'];
function fileManager($q, fileManagerClient, appInfo) {
var service = {
files: [],
load: load,
upload: upload,
remove: remove,
fileExists: fileExists,
status: {
uploading: false
}
};
return service;
function load() {
//Do Stuff
}
function upload(files) {
//ToDo Check that file extension exists here
service.status.uploading = true;
appInfo.setInfo({ busy: true, message: "uploading files" });
var formData = new FormData();
angular.forEach(files, function (file) {
formData.append(file.name, file);
});
return fileManagerClient.save(formData)
.$promise
.then(function (result) {
if (result && result.files) {
result.files.forEach(function (file) {
if (!fileExists(file.name)) {
service.files.push(file);
}
});
}
appInfo.setInfo({ message: "files uploaded successfully" });
return result.value;
},
function (result) {
appInfo.setInfo({ message: "something went wrong: " + result.data.message });
return $q.reject(result);
})
['finally'](
function() {
appInfo.setInfo({ busy: false });
service.status.uploading = false;
});
}
function remove(file) {
//Do Stuff
}
function fileExists(fileName) {
//Do Stuff
}
}
})();
我覺得你可以直接注入* $ rootScope *喜歡* $ q * – NNR