简体   繁体   English

使用全局模块声明时无法识别Angular Service

[英]Angular Service not recognized when declared with global module

I have an angular application that has a main module called app and then child modules: app.ReportingApp, app.MediaJobApp, app.ManageUsers. 我有一个角度应用程序,该应用程序有一个名为app的主模块,然后是子模块:app.ReportingApp,app.MediaJobApp,app.ManageUsers。

I have a service that allows some alerts to be show from local storage and what not. 我有一项服务,可以从本地存储中显示某些警报,而不能显示其他警报。

The issue I have found is that when I inject the service dependency into the controller I want to use from a child module it breaks. 我发现的问题是,当我将服务依赖项注入到要从子模块使用的控制器中时,它就中断了。 Yet when I declare 'app.Reporting' in my service instead of 'app', it works but breaks things else where. 但是,当我在服务中声明“ app.Reporting”而不是“ app”时,它可以工作,但会破坏其他地方的东西。

Am I not able to use a service like this? 我不能使用这样的服务吗? Logically it makes sense to have the main module being called in the service and the child modules being able to call a service related to the main module but it is looking like I may have to make another service to satisfy the separation between modules. 从逻辑上讲,在服务中调用主模块是有意义的,而子模块能够调用与主模块相关的服务,但是看起来我可能必须制造另一个服务来满足模块之间的分隔。

Here is some code: 这是一些代码:

app.js: app.js:

    angular.module('app', ['app.MediaJobApp', 'app.ManageUsers', 'app.ReportingApp'])
.run(function ($http) {
    $http.defaults.headers.common['X-XSRF-Token'] =
        document.getElementsByName('__RequestVerificationToken')[0].getAttribute("value");
});

app.ReportingApp.js: app.ReportingApp.js:

angular.module('app.ReportingApp', []);

PageAlertService.js: PageAlertService.js:

angular.module('app').service('PageAlertService', function () {

    this.setAlert = function() {
        console.log("In setAlert");
        if (localStorage.getItem("Success")) {
            var alertObj = {
                alert: "Success",
                alertMessage: localStorage.getItem("Success")
            };
            console.log(alertObj);
        } else if (localStorage.getItem("Error") && localStorage.getItem("Error") != null) {
            var alertObj = {
                alert: "Error",
                alertMessage: localStorage.getItem("Error")
            };

        };
        return alertObj;
    };

    this.errorStatusCheck = function(error, successString) {
        if (error.status = -1) {
            localStorage.setItem("Success", successString);
        } else {
            localStorage.setItem("Error", "Error occured: " + error.status + error.statusText);
        };
    };

});

and top of ReportingCtrl.js: 以及ReportingCtrl.js的顶部:

angular.module('app.ReportingApp').controller('ReportingCtrl',
    [
        '$scope',
        'ReportingService',
        'PageAlertService',
        function ($scope, ReportingService, PageAlertService) {

The error I get is: Error: [$injector:unpr] Unknown provider: PageAlertServiceProvider <- PageAlertService <- ReportingCtrl , Which I have found to be due to the issue I have talked about above. 我得到的错误是: Error: [$injector:unpr] Unknown provider: PageAlertServiceProvider <- PageAlertService <- ReportingCtrl ,我发现这是由于上面提到的问题所致。

You simply cannot use services like this. 您根本无法使用这样的服务。 You must have to inject the module of the service into the module of your controller. 您必须必须将服务模块注入控制器的模块中。 In this case, the solution might be separating the service into another module: 在这种情况下,解决方案可能是将服务分成另一个模块:

angular.module('app.service').service('PageAlertService', function () {

    this.setAlert = function() {
        console.log("In setAlert");
        if (localStorage.getItem("Success")) {
            var alertObj = {
                alert: "Success",
                alertMessage: localStorage.getItem("Success")
            };
            console.log(alertObj);
        } else if (localStorage.getItem("Error") && localStorage.getItem("Error") != null) {
            var alertObj = {
                alert: "Error",
                alertMessage: localStorage.getItem("Error")
            };

        };
        return alertObj;
    };

    this.errorStatusCheck = function(error, successString) {
        if (error.status = -1) {
            localStorage.setItem("Success", successString);
        } else {
            localStorage.setItem("Error", "Error occured: " + error.status + error.statusText);
        };
    };

});

Then when creating the app.ReportingApp module you have to provide this service module as a dependency: 然后,在创建app.ReportingApp模块时,必须提供此服务模块作为依赖项:

angular.module('app.ReportingApp',['app.service']);

Then you can use the following code without error: 然后,您可以使用以下代码而不会出现错误:

angular.module('app.ReportingApp').controller('ReportingCtrl',
    [
        '$scope',
        'ReportingService',
        'PageAlertService',
        function ($scope, ReportingService, PageAlertService) {

Hope you will understand. 希望你能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM