I have an existing Angular Factory and I'm having some trouble inserting some new code that needs to pass the results of a $resource get to my Angular Controller. I'm trying to do this in the "sendFilesToController()" function shown below. I'm not sure of the syntax. Any help is appreciated.
Thanks,
Pete
My factory looks like this:
(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;
}
}
}
}
})();
and in my controller I just have this:
$scope.files = fileManagerClient.sendFiles(response);