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

时间:2016-06-23 18:31:25

标签: angularjs model-view-controller module controller

我在window.onload中调用了Angular的初始化。

我的App2.js文件(带有window.onload func。引用)

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

我的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>

我在浏览器控制台中收到此错误:

angular.js:68未捕获错误:[$ injector:modulerr]无法实例化模块mainModule,原因如下: 错误:[$ injector:nomod]模块'mainModule'不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

enter image description here

显然,调用“message”模型不会在视图中执行/ bind。我对控制器和模块的命名参考是正确的。 所以我决定尝试创建角度模块,不用 window.onload 函数引用,它有效:

我的App2.js文件(没有window.onload func。引用)

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

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

1 个答案:

答案 0 :(得分:0)

ng-app会立即加载模块。它不会等到dom准备好了。

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