我有一个现有的Angular Factory,我在插入一些需要将$ resource的结果传递给我的Angular Controller的新代码时遇到了一些麻烦。我正在尝试在下面显示的“sendFilesToController()”函数中执行此操作。我不确定语法。任何帮助表示赞赏。
谢谢,
皮特
我的工厂看起来像这样:
(function () {
'use strict';
angular
.module('ooApp.controllers')
.factory('fileManager', fileManager);
fileManager.$inject = ['$q', 'fileManagerClient', 'appInfo', 'sharedPropertiesSvc', 'FileSvc', '$location', 'growl', ];
function fileManager($q, fileManagerClient, appInfo, sharedPropertiesSvc, FileSvc, $location, growl) {
var service = {
files: [],
load: load,
upload: upload,
remove: remove,
fileExists: fileExists,
pass: pass,
status: {
uploading: false
}
};
return service;
function load() {
appInfo.setInfo({ busy: true, message: "loading files" })
service.files.length = 0;
return fileManagerClient.query()
.$promise
.then(function (result) {
result.files
.forEach(function (file) {
service.files.push(file);
});
appInfo.setInfo({ message: "files loaded successfully" });
return result.$promise;
},
function (result) {
appInfo.setInfo({ message: "something went wrong: " + result.data.message });
return $q.reject(result);
})
['finally'](
function () {
appInfo.setInfo({ busy: false });
});
}
function upload(files) {
//ToDo Check that file extension exists here
item.OversightId = oversightId;
FileSvc.update({ Id: oversightId }, item).$promise.then(function (response) {
var x = 5;
sendFilesToController(id);
});
return result.$promise;
},
function (error) {
appInfo.setInfo({ message: "something went wrong: " + error.data.message });
sharedPropertiesSvc.setDuplicateFileSwitch(true);
sharedPropertiesSvc.setDupFileMsg(error.data.error.message);
var url = '/oa/?id=' + oversightId;
$location.url(url);
return $q.reject(error);
})
['finally'](
function () {
appInfo.setInfo({ busy: false });
service.status.uploading = false;
});
}
function sendFilesToController(id) {
//appInfo.setInfo({ busy: true, message: "deleting file " + file.ImageName });
return {
sendFiles: function () {
//return FileSvc.getById({ Id: oversightId }).$promise.then(function (response) {
//});
var files = {};
FileSvc.getById({ Id: id }).success(function (response) {
angular.copy(response, files);
});
return files;
}
}
}
}
})();
在我的控制器中我只有这个:
$scope.files = fileManagerClient.sendFiles(response);
答案 0 :(得分:0)
定义factory =>
然后注入工厂=>进入你想要的控制器
angular.module('myExample', [])
.controller('myCtrl', function($scope, Factory) {
console.log('Factory', Factory.word);
});
// factory
angular.module('myExample')
.factory('Factory', function () {
var factory = { };
factory.word = 'example';
return factory;
})