如何将 $rootScope 注入 AngularJS 工厂
How to inject $rootScope into AngularJS factory
我对 Angular 比较陌生,无法找到关于如何将 $rootScope 注入像我这样组织的工厂函数的确切答案。所以任何帮助表示赞赏。基本上,我只是想注入 $rootScope,因为我想在函数 upload(files) 中使用它。 . . .谢谢皮特
这是我的工厂(为了简洁省略了一些代码):
(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
}
}
})();
这与您通常注入依赖项的方式相同,
fileManager.$inject = ['$rootScope','$q', 'fileManagerClient', 'appInfo']
function fileManager($rootScope,$q, fileManagerClient, appInfo) {
我对 Angular 比较陌生,无法找到关于如何将 $rootScope 注入像我这样组织的工厂函数的确切答案。所以任何帮助表示赞赏。基本上,我只是想注入 $rootScope,因为我想在函数 upload(files) 中使用它。 . . .谢谢皮特
这是我的工厂(为了简洁省略了一些代码):
(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
}
}
})();
这与您通常注入依赖项的方式相同,
fileManager.$inject = ['$rootScope','$q', 'fileManagerClient', 'appInfo']
function fileManager($rootScope,$q, fileManagerClient, appInfo) {