0

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);

1 Answer 1

0

you misunderstanding the factory and controller usage....

define the factory =>

then inject the factory => into the controller that you want

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;
})
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Joseph. I understand what you are saying. However, I'm not sure how to do it in the specific code that already exists.
just inject the factory and use the service , you could think the factory is the common lib for you
What i really want to do here is bind the files returned in the factory to $scope.item.Files in the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.