Javascript 使用全局模块声明时无法识别角度服务

Javascript 使用全局模块声明时无法识别角度服务,javascript,angularjs,Javascript,Angularjs,我有一个angular应用程序,它有一个名为app的主模块,然后是子模块:app.ReportingApp、app.MediaJobApp、app.ManageUsers 我有一项服务,允许从本地存储显示一些警报,而不是其他 我发现的问题是,当我将服务依赖项注入到我希望从子模块使用的控制器时,它会中断。然而,当我在我的服务中声明“app.Reporting”而不是“app”时,它起作用,但在其他地方破坏了东西 我不能使用这样的服务吗?从逻辑上讲,让主模块在服务中被调用,而子模块能够调用与主模块相

我有一个angular应用程序,它有一个名为app的主模块,然后是子模块:app.ReportingApp、app.MediaJobApp、app.ManageUsers

我有一项服务,允许从本地存储显示一些警报,而不是其他

我发现的问题是,当我将服务依赖项注入到我希望从子模块使用的控制器时,它会中断。然而,当我在我的服务中声明“app.Reporting”而不是“app”时,它起作用,但在其他地方破坏了东西

我不能使用这样的服务吗?从逻辑上讲,让主模块在服务中被调用,而子模块能够调用与主模块相关的服务是有意义的,但看起来我可能需要创建另一个服务来满足模块之间的分离

下面是一些代码:

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:

angular.module('app.ReportingApp', []);
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);
        };
    };

});
和ReportingCtrl.js的顶部:

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

我得到的错误是:
error:[$injector:unpr]未知提供程序:PageAlertServiceProvider您不能使用这样的服务。您必须将服务的模块注入控制器的模块。在这种情况下,解决方案可能是将服务分离到另一个模块中:

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);
        };
    };

});
然后,在创建
app.ReportingApp
模块时,您必须将此服务模块作为依赖项提供:

angular.module('app.ReportingApp',['app.service']);
然后您可以使用以下代码而不会出错:

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

希望您理解。

您的应用程序模块依赖于app.ReportingApp模块(反之亦然),因此您将无法访问app.ReportingApp中app模块中定义的服务。或者为你的服务定义一个seprate模块,比如app.DataService,并将其作为依赖项添加到你的app.ReportingApp模块我创建了一个plnkr,我相信它演示了你的用例,并表明它确实按照预期工作。您可以在devtools中查看console.log。谢谢你们,我现在明白了!是的,我完全理解。一位绅士在我的帖子上评论了你现在说的话。我不知道你不能这样做,因为在其他语言中情况并非如此。谢谢你的帖子!