使用windows .onload初始化模块时,模块不可用。角抛出错误

时间:2022-05-11 16:56:32

I'm calling the initialization of Angular in window.onload.

我在windows。onload中调用角的初始化。

my App2.js file (with window.onload func. reference)

App2。js文件(与窗口。onload函数。引用)

window.onload = function () {
  var mainModule = angular.module("mainModule", []);
  mainModule.controller("myController", function ($scope) {
    $scope.message = "XXXX...Angular JS merda...XXXX";
  });
};

my html layout:

我的html布局:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>BraavosLand | Starter</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!--Culture has been set in base controller in method OnActionExecuting()-->
    @Html.Raw(ViewBag.MetaCulture)

    <!-- Bootstrap 3.3.5 -->
    <link href="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <!-- Font Awesome -->
    <link href="~/Scripts/PlugIns_ThirdParty/font-awesome-4.5.0/css/font-awesome.min.css" rel="stylesheet" />
    <!-- Ionicons -->
    <link href="~/Scripts/PlugIns_ThirdParty/ionicons-2.0.1/css/ionicons.min.css" rel="stylesheet" />
    <!-- Theme style -->
    <link href="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/dist/css/AdminLTE.min.css" rel="stylesheet" />
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect.
    -->
    <link href="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/dist/css/skins/skin-blue.min.css" rel="stylesheet" />
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <!--This block was include in the kendo template at start up-->

    <link href="@Url.Content("~/Content/kendo/2016.1.112/kendo.common.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2016.1.112/kendo.mobile.all.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2016.1.112/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/kendo/2016.1.112/kendo.bootstrap.min.css")" rel="stylesheet" type="text/css" />

    <link href="@Url.Content("~/Content/kendo/2016.1.112/kendo.dataviz.default.min.css")" rel="stylesheet" type="text/css" />
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/jszip.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/kendo.all.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/kendo.aspnetmvc.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script>




    <!--BELOW ARE ALL REference added by me-->
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/cultures/kendo.culture.en-GB.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/cultures/kendo.culture.en-US.min.js")"></script>
    <script src="@Url.Content("~/Scripts/kendo/2016.1.112/cultures/kendo.culture.it-IT.min.js")"></script>



    <script src="~/Scripts/PlugIns_ThirdParty/blockUI/jquery.blockUI.js"></script>
    <script src="~/Scripts/PlugIns_ThirdParty/spin_js/spin.min.js"></script>


    <!-- Angular -->
    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular-route.js"></script>
    <script src="~/Scripts/angular-resource.js"></script>
    <script src="~/App_ng/App2.js"></script>


    <!-- Bootstrap 3.3.5 -->
    <script src="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/bootstrap/js/bootstrap.min.js"></script>


    <!--SweetAlert-->
    <link href="@Url.Content("~/Scripts/PlugIns_ThirdParty/sweetalert2-master/dist/sweetalert2.min.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/PlugIns_ThirdParty/sweetalert2-master/dist/sweetalert2.min.js")"></script>


    <!-- AdminLTE App -->
    <script src="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/dist/js/app.min.js"></script>






    <!--My scripts-->
    <link href="@Url.Content("~/Scripts/MyScripts/zr_main_style.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/MyScripts/zr_main_js.js")"></script>






    <!--This is for Kendo to set the culture-->
    <script type="text/javascript">

        function getCultureFromMetaTag() {
            var cultureMetaData = $("meta[name='accept-language']").attr("content");

            return cultureMetaData;
        }

        kendo.culture(getCultureFromMetaTag());
        //kendo.culture("en-US");

        console.log("culture from meta tag: " + getCultureFromMetaTag());
    </script>





    <!--This is for Azure Application Insights-->
    <script type="text/javascript">
        var appInsights = window.appInsights || function (config) {
            function r(config) {
                t[config] = function () {
                    var i = arguments;
                    t.queue.push(function () { t[config].apply(t, i) })
                }
            }

            var t = { config: config }, u = document, e = window, o = "script", s = u.createElement(o), i, f;
            for (s.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace"]; i.length;) r("track" + i.pop());
            return r("setAuthenticatedUserContext"), r("clearAuthenticatedUserContext"), config.disableExceptionTracking || (i = "onerror", r("_" + i), f = e[i], e[i] = function (config, r, u, e, o) {
                var s = f && f(config, r, u, e, o);
                return s !== !0 && t["_" + i](config, r, u, e, o), s
            }), t
        }({ instrumentationKey: "a0b580d2-a9b1-4016-8f89-88fe1f7c347c" });
        window.appInsights = appInsights;
        appInsights.trackPageView();
    </script>



</head>
<!--
BODY TAG OPTIONS:
=================
Apply one or more of the following classes to get the
desired effect
|---------------------------------------------------------|
| SKINS         | skin-blue                               |
|               | skin-black                              |
|               | skin-purple                             |
|               | skin-yellow                             |
|               | skin-red                                |
|               | skin-green                              |
|---------------------------------------------------------|
|LAYOUT OPTIONS | fixed                                   |
|               | layout-boxed                            |
|               | layout-top-nav                          |
|               | sidebar-collapse                        |
|               | sidebar-mini                            |
|---------------------------------------------------------|
-->
<body class="hold-transition skin-blue sidebar-mini" ng-app="mainModule">
    <div class="wrapper">
        <!-- Main Header -->
        <header class="main-header">
            <!-- Logo -->
            <a href="@Url.Action("Index", "Home", new { }, protocol: Request.Url.Scheme)" class="logo">
                <!-- mini logo for sidebar mini 50x50 pixels -->
                <span class="logo-mini"><b>BL</b></span>
                <!-- logo for regular state and mobile devices -->
                <span class="logo-lg"><b>B</b>raavos<b>L</b>and</span>
            </a>
            <!-- Header Navbar -->
            <nav class="navbar navbar-static-top" role="navigation">
                <!-- Sidebar toggle button-->
                <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
                    <span class="sr-only">Toggle navigation</span>
                </a>
                <!-- Navbar Right Menu -->
                <div class="navbar-custom-menu">
                    <ul class="nav navbar-nav">
                        @if (Request.IsAuthenticated)
                        {


                            <!-- Messages: style can be found in dropdown.less-->
                            {
                                Html.RenderAction("_NavBarMessages", "Layout");
                            }

                            <!-- Notifications Menu -->
                            {
                                Html.RenderAction("_NavBarNotifications", "Layout");
                            }

                            <!-- Tasks Menu -->
                            {
                                Html.RenderAction("_NavBarTasks", "Layout");
                            }

                            <!-- User Account Menu -->
                            {
                                Html.RenderAction("_NavBarUserAccount", "Layout");
                            }

                            <!-- Control Sidebar Toggle Button -->
                            {
                                Html.RenderAction("_NavBarBtnToggleSideBar", "Layout");
                            }
                        }
                    </ul>
                </div>
            </nav>
        </header>
        <!-- Left side column. contains the logo and sidebar -->
        <aside class="main-sidebar">
            <!-- sidebar: style can be found in sidebar.less -->
            <section class="sidebar">
                <!-- Sidebar user panel (optional) -->
                <div class="user-panel">
                    <div class="pull-left image">
                        <img src="~/Scripts/PlugIns_ThirdParty/Theme-AdminLTE-2.3.0/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
                    </div>
                    <div class="pull-left info">
                        <p>Alexander Pierce</p>
                        <!-- Status -->
                        <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
                    </div>
                </div>
                <!-- search form (Optional) -->
                <form action="#" method="get" class="sidebar-form">
                    <div class="input-group">
                        <input type="text" name="q" class="form-control" placeholder="Search...">
                        <span class="input-group-btn">
                            <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </form>
                <!-- /.search form -->
                <!-- Sidebar Menu -->
                <ul class="sidebar-menu">


                    @{Html.RenderAction("_SideBarLeftMenu", "Layout");}





                </ul><!-- /.sidebar-menu -->
            </section>
            <!-- /.sidebar -->
        </aside>
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <h1>
                    Page Header
                    <small>Optional description</small>
                </h1>
                <ol class="breadcrumb">
                    <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
                    <li class="active">Here</li>
                </ol>
            </section>
            <!-- Main content -->
            <section class="content">
                <div ng-controller="myController">
                    {{message}}
                </div>
                <!-- Your Page Content Here -->
                <!-- Your Page Content Here -->
                @RenderBody()
            </section><!-- /.content -->
        </div><!-- /.content-wrapper -->
        <!-- Main Footer -->
        <footer class="main-footer">
            <!-- To the right -->
            <div class="pull-right hidden-xs">
                Anything you want
            </div>
            <!-- Default to the left -->
            <strong>Copyright &copy; 2015 <a href="#">Company</a>.</strong> All rights reserved.
        </footer>



        @if (Request.IsAuthenticated)
        {
        <!-- Control Sidebar -->
        <!-- Right side -->
            {
                Html.RenderAction("_SideBarRightControl", "Layout");
            }
        }


    </div>



    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>










</body>
</html>

I get this error in the browser console:

我在浏览器控制台得到这个错误:

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module mainModule due to: Error: [$injector:nomod] Module 'mainModule' 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.

角。未捕获错误:[$injector:modulerr]未能实例化模块主模块,原因是:错误:[$injector:nomod]模块‘mainModule’不可用!您要么拼错了模块名称,要么忘记加载它。如果注册一个模块,请确保将依赖项指定为第二个参数。

使用windows .onload初始化模块时,模块不可用。角抛出错误

Clearly calling the "message" model does not execute/bind in the view. My naming references for the controller and module are correct. So I decided to try and create the angular module without the window.onload function reference, and it worked:

显然,调用“message”模型不会在视图中执行/绑定。我对控制器和模块的命名引用是正确的。所以我决定试着创建一个没有窗户的角度模块。onload函数引用,其工作原理如下:

my App2.js file (no window.onload func. reference)

App2。js文件(没有窗户。onload函数。引用)

var mainModule = angular.module("mainModule", []);
mainModule.controller("myController", function ($scope) {
    $scope.message = "XXXX...Angular JS tutorial merda...XXXX ciaooo";
});

Question: What is it that is preventing me from creating a module after the document has loaded by using window.onload?

问题:是什么原因阻止我在使用windows .onload加载文档之后创建模块?

1 个解决方案

#1


0  

ng-app will load the module right away. It won't wait till the dom is ready.

ng-app会马上加载这个模块。它不会等待dom就绪。

You can do the same things as ng-app by calling angular.bootstrap. Or just completely drop the load event handling. Angular will defer initializing till later.

您可以通过调用angular.bootstrap执行与ng-app相同的操作。或者完全放弃加载事件处理。角将延迟初始化到稍后。

#1


0  

ng-app will load the module right away. It won't wait till the dom is ready.

ng-app会马上加载这个模块。它不会等待dom就绪。

You can do the same things as ng-app by calling angular.bootstrap. Or just completely drop the load event handling. Angular will defer initializing till later.

您可以通过调用angular.bootstrap执行与ng-app相同的操作。或者完全放弃加载事件处理。角将延迟初始化到稍后。