angularjsangularjs-rootscope

How to inject $rootScope into AngularJS factory


I'm relatively new to Angular and wasn't able to find an exact answer for how to inject $rootScope into a factory function that's organized like mine. So any help is appreciated. Basically, I'm just trying to inject $rootScope because I'd like to use it in the function upload(files). . . . Thanks Pete

Here is my factory (some code omitted for brevity):

(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
        }



    }
})();

Solution

  • It's the same way that you normally inject dependencies,

     fileManager.$inject = ['$rootScope','$q', 'fileManagerClient', 'appInfo']
     function fileManager($rootScope,$q, fileManagerClient, appInfo) {