【问题标题】:AngularJs $injector Error in my App我的应用程序中的 AngularJs $injector 错误
【发布时间】:2017-03-01 20:38:30
【问题描述】:

由于Error: $injector:modulerr Module Error,我目前遇到了我的应用程序崩溃的问题

完整的错误是:

    Failed to instantiate module app due to:
    Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module JobCtrl due to:
Error: [$injector:nomod] Module 'JobCtrl' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.6.2/$injector/nomod?p0=JobCtrl
    at http://ep-dev3/CPDManagement/app/Vendor/angular.js:69:20
    at http://ep-dev3/CPDManagement/app/Vendor/angular.js:2188:31
    at ensure (http://ep-dev3/CPDManagement/app/Vendor/angular.js:2112:46)
    at module (http://ep-dev3/CPDManagement/app/Vendor/angular.js:2186:24)
    at http://ep-dev3/CPDManagement/app/Vendor/angular.js:4757:36
    at forEach (http://ep-dev3/CPDManagement/app/Vendor/angular.js:358:34)
    at loadModules (http://ep-dev3/CPDManagement/app/Vendor/angular.js:4741:13)
    at http://ep-dev3/CPDManagement/app/Vendor/angular.js:4758:54
    at forEach (http://ep-dev3/CPDManagement/app/Vendor/angular.js:358:34)
    at loadModules (http://ep-dev3/CPDManagement/app/Vendor/angular.js:4741:13)
http://errors.angularjs.org/1.6.2/$injector/modulerr?

我的 app.js 和控制器中都有 ui.bootstrap 作为依赖项。当我尝试从我的控制器中删除依赖项时,应用程序会中断。如果我离开它,它运行良好,但给我添加服务和指令的问题。

app.js:

    angular.module('app',
    [
        'JobCtrl',
        'JobSvc',
        'WebsiteCtrl',
        'WebsiteSvc',
        'myClientCtrl',
        'ClientSvc',
        'MediaCompanyCtrl',
        'MediaCompanySvc',
        'PageAlertSvc',
        'ui.bootstrap',
        'Common'
    ]
);

控制器:

    angular.module('app')
    .controller('JobCtrl',
            [
                'JobService',
                'WebsiteService',
                'MediaCompanyService',
                'ProductService',
                '$scope',
                '$uibModal',
                'PageAlertService',
                function (JobService, WebsiteService, MediaCompanyService,
                    ProductService, $scope, $uibModal,PageAlertService){
                                     /** Stuff in my controller **/
             }]);

编辑显示来源

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - CPD Management Tool</title>
    <script src="~/app/Vendor/angular.min.js"></script>
    <script src="~/Scripts/ui-bootstrap-tpls-2.5.0.min.js"></script>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
@if (User.Identity.IsAuthenticated )
{
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                @Html.ActionLink("Clear Path Direct", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
               @if (User.IsInRole("Admin"))
               {
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                           aria-haspopup="true" aria-expanded="false">Tools<span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li class="text-center">@Html.ActionLink("Media Jobs", "Index", "MediaJobs")</li>
                            <li class="text-center">Order Processing</li>
                            <li class="text-center">Media Reporting</li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                           aria-haspopup="true" aria-expanded="false">Manage<span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li class="text-center">@Html.ActionLink("Manage Clients", "Index", "Clients")</li>
                            <li class="text-center">@Html.ActionLink("Manage Media Companies", "Index", "MediaCompanies")</li>
                            <li class="text-center">@Html.ActionLink("Manage Websites", "Index", "Websites")</li>
                        </ul>
                    </li>

                </ul>
               }
                @Html.Partial("_LoginPartial")

            </div>
        </div>
    </div>
                }
    <div class="container body-content">
        @RenderBody()
        <!--
        <footer>
            <p>&copy; @DateTime.Now.Year - CPD Management Tool</p>
        </footer>
            -->
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    <!--angular scripts-->
    @Scripts.Render("~/bundles/Angular")
    <!-- ---->
    @RenderSection("scripts", required: false)
</body>
</html>

为什么我的应用程序在我的控制器运行时运行:

angular.module('app', ['ui.bootstrap']).controller('JobCtrl',
        [
            'JobService',
            'WebsiteService',
            'MediaCompanyService',
            'ProductService',
            '$scope',
            '$uibModal',
            'PageAlertService',
            function (JobService, WebsiteService, MediaCompanyService,
                ProductService, $scope, $uibModal,PageAlertService)

当我有它时打破:

angular.module('app').controller('JobCtrl',
    [
        'JobService',
        'WebsiteService',
        'MediaCompanyService',
        'ProductService',
        '$scope',
        '$uibModal',
        'PageAlertService',
        function (JobService, WebsiteService, MediaCompanyService,
            ProductService, $scope, $uibModal,PageAlertService)

非常感谢您的帮助!

【问题讨论】:

  • 你想删除引导程序和相关的东西吗?
  • 我们能看到你在 html 中加载 js 的位置吗?
  • 我希望能够使用 ui.bootstrap,但是如果依赖项在 app.js 依赖项列表中,我应该仍然可以使用它,对吧?
  • 您将 JobCtrl 列为第一个 module 以在 app.js 中导入,但 JobCtrl 不是模块,它是一个控制器。删除 app.js 中的第一行。
  • 就此而言,您只需要导入在不同模块中声明的服务,即您要导入的模块,而不是服务本身。

标签: angularjs angularjs-injector


【解决方案1】:

我遇到的问题是我在应用程序模块 (app.js) 中导入了所有控制器和服务。感谢@Claies 向我指出这一点。

错误:

      angular.module('app',
    [
        'JobCtrl',
        'JobSvc',
        'WebsiteCtrl',
        'WebsiteSvc',
        'myClientCtrl',
        'ClientSvc',
        'MediaCompanyCtrl',
        'MediaCompanySvc',
        'PageAlertSvc',
        'ui.bootstrap',
        'Common'
    ]
);

右:

angular.module('app', ['ui.bootstrap']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2015-06-11
    • 2017-06-27
    • 2014-12-25
    • 2016-07-05
    相关资源
    最近更新 更多