在另一个工厂中使用工厂是未定义的-AngularJS

时间:2019-04-26 07:45:05

标签: angularjs

我有一个名为ReportServiceIndexService的工厂。我想在IndexService内使用ReportService

但是在ReportService中,它表示IndexServiceundefined。我不太确定为什么。

到目前为止,这是我的代码:

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'])

我的index.html脚本导入

<!-- 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>

请帮助。我已经花了2多个小时了,但仍然找不到问题。。在此先感谢:)

1 个答案:

答案 0 :(得分:1)

您需要先注入它,例如:

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

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

至少这是我的一种方式。