Ken Flake
Ken Flake

Reputation: 595

Using factory in another factory is undefined - AngularJS

I have a factories called ReportService and IndexService. I want to use IndexService inside of ReportService.

But in ReportService, it says that the IndexService is undefined. I'm not quite sure why.

Here's my code so far:

indexService.js

angular.module('IndexService', []).factory('IndexService', ['$http', function ($http) {
    return {
        // Sending npm config from server (node) to front-end as JSON so we can use it in front-end.
        // See localhost.json.
        getConfig: function() {
            return $http.get("/api/get-config");
        }
    }
}]);

reportService.js

angular.module('ReportService', []).factory('ReportService', ['$http', 'IndexService', function ($scope, $http, IndexService) {
    // Sending npm config from server (node) to front-end as JSON so we can use it in front-end.
    // See localhost.json.
    IndexService.getConfig()
    .then(function(response) {
        var configs = response.data
    }); 

    return {
        generateExcelReport: function(searchCriteriaList) {
            var requestConfig = {
                responseType: "arraybuffer",
                headers: { "Content-Disposition": "attachment" }
            };

            // I want to call IndexService.getConfig()
            // so I can change my base URL and port based on environment. My configs are in node.js back-end
            var url = configs.url;
            var port = configs.port;

            return $http.post(url + ":" port + "/my-api-link", searchCriteriaList, requestConfig);
        },
    }
}]);

app.js

angular.module('myApp', ['ngStorage', 'ngRoute', 'appRoutes', 'IndexController', 'IndexService', 'ReportController', 'ReportService', 'PackageController', 'PackageService', 'FarmService', 'DesignService', 'UserService', 'oitozero.ngSweetAlert', 'ui.select', 'ui.materialize', 'ngSanitize', 'ngFileSaver'])

my index.html script imports

<!-- Our Angular Controllers and Services JS -->
<script src="./js/controllers/indexController.js"></script>
<script src="./js/controllers/reportController.js"></script>
<script src="./js/controllers/nerdController.js"></script>
<script src="./js/controllers/packageController.js"></script>

<script src="./js/services/indexService.js"></script>
<script src="./js/services/reportService.js"></script>
<script src="./js/services/farmService.js"></script>
<script src="./js/services/packageService.js"></script>
<script src="./js/services/designService.js"></script>
<script src="./js/services/userService.js"></script>

<script src="./js/appRoutes.js"></script>
<script src="./js/app.js"></script>

Kindly help. I've been over 2 hours on this and I can't still find the problem.. Thanks in advance :)

Upvotes: 0

Views: 69

Answers (1)

Andris
Andris

Reputation: 4193

You need to inject it first, like:

angular
    .module('ReportService')
    .factory('ReportService', ReportService);

ReportService.$inject = ['IndexService'];
function ReportService(IndexService) {
// You code blah blah here
}

At least this is a way, how i am doing.

Upvotes: 1

Related Questions