Search code examples
angularjsrequirejsrestangular

Angular service which is a require js module, is not getting the injected dependencies


I have a module 'global.services' in which I registered a service "Restservices"

(function(define, angular) {
"use strict";

define(["global/services/restServices"],
    function(RestServices) {
        var moduleName = "global.services";

        angular.module(moduleName, [])
            .config(function(RestangularProvider) {
                RestangularProvider.setBaseUrl('http://localhost:8888/src/app/data/');
                RestangularProvider.setRequestSuffix('.json');
            })
            .factory('RestServices', RestServices);
        return moduleName;
    });
}(define, angular));

and my "RestService" is also a module

(function(define, angular) {
"use strict";

define(function() {
    var restService = function($rootScope, Restangular) {
        // body...

        return {
            getData: function(arg) {
                // body...
                $rootScope.$broadcast('gettingData');
                return Restangular.oneUrl('listPanel');
            }
        }
    };
    return restService;
 });
}(define, angular));

In the above service both $rootScope and Restangular are undefined. Please tell me how to inject dependencies in this respect.


Solution

  • I found the reason of $rootScope and Restangular being undefined.

    Factory registration is perfect but when I am trying to use it in a different module's controller I am injecting "global/services/restServices" instead of "global/services/services" which is the actual services module.

    Previous code:

    (function(define, angular) {
    define(['global/services/restServices'], function(RestServices) {
        var HeaderController = function($scope, $rootScope, $translate, $location, $document, RestServices) {
    
            var user_modules = window.SESSION.getModules(),
                modules = [];
    
            angular.forEach(user_modules, function(moduleName) {
                modules.push(window.CONFIG.MODULES[moduleName]);
            });
    
            RestServices.getData('webAppsData.json').get().then(function(d) {
                if (d) {
                    if (d.response) {
                        $scope.webApps = d.response;
                    } else {
                        $scope.webApps = [];
                    }
                } else {
                    $scope.webApps = [];
                }
            });
            $scope.title = "Tumbler";
            $scope.modules = modules;
        };
        return ["$scope", "$rootScope", "$translate", "$location", "$document", "RestServices", HeaderController];
    })
    }(define, angular));
    

    Present code(working):

    (function(define, angular) {
    define(['global/services/services'], function() {
        var HeaderController = function($scope, $rootScope, $translate, $location, $document, RestServices) {
    
            var user_modules = window.SESSION.getModules(),
                modules = [];
    
            angular.forEach(user_modules, function(moduleName) {
                modules.push(window.CONFIG.MODULES[moduleName]);
            });
    
            RestServices.getData('webAppsData.json').get().then(function(d) {
                if (d) {
                    if (d.response) {
                        $scope.webApps = d.response;
                    } else {
                        $scope.webApps = [];
                    }
                } else {
                    $scope.webApps = [];
                }
            });
            $scope.title = "Tumbler";
            $scope.modules = modules;
        };
        return ["$scope", "$rootScope", "$translate", "$location", "$document", "RestServices", HeaderController];
    })
    }(define, angular));
    

    So, just inject the module and use its services.