【问题标题】:angularjs weird error loading controllersangularjs奇怪的错误加载控制器
【发布时间】:2016-02-29 13:49:42
【问题描述】:

过去几个月我一直在开发 angularjs,并且我有一个包含一些控制器、指令和服务的应用程序。到目前为止,一切都很好,但是使用最后一个控制器时,我遇到了一个奇怪的错误,这让我发疯了。因为该应用程序足够大,可以在这里放置所有代码,所以我将放置重要部分。

在我的 index.html 中

<!DOCTYPE html>
<html ng-app="my_app">
<head>
    <meta charset="encoding">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <meta name="viewport"  content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link rel="stylesheet" href="lib/ionic/css/ionic.css">
    <link rel="stylesheet" href="css/style.css">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/angular-translate/angular-translate.min.js"></script>
    <script src="lib/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <!--<script src=""-->
    <script src="cordova.js"></script>


    <!-- your app's js -->
    <script src="js/app.js"></script>
    <!--SERVICES-->
    <script src="js/services/services.js"></script>
    <script src="js/services/s_configDB.js"></script>
    <script src="js/services/s_clinicDB.js"></script>
    <!-- CONTROLLERS -->
    <script src="js/controllers/controllers.js"></script>
    <script src="js/controllers/c_splash.js"></script>
    <script src="js/controllers/c_communications.js"></script>
    <!--<script src="js/controllers/c_communicationsB.js"></script> 
    <script src="js/controllers/c_clock.js"></script>
    <script src="js/controllers/c_menu.js"></script>
    <script src="js/controllers/c_language.js"></script>
    <!--DIRECTIVES-->
    <script src="js/directives/directives.js"></script>
    <script src="js/directives/d_menu.js"></script>

</head>

<body ng-controller="AppComms">
    <ion-nav-view ></ion-nav-view>
</body>
</html>

At my app.js:

var SDv4 = angular.module('my_app', [
    'ionic',
    'my_app.services',
    'my_app.controllers',
    'my_app.directives'
]);

在 controllers.js 中

var app = angular.module('my_app.controllers',[
    //'ionic',
    'my_app.c_splash',
    'my_app.c_clock',
    'my_app.c_language',
    'my_app.c_comms',
    //'my_app.c_commsb',
    'pascalprecht.translate',
    'my_app.c_menu'
]);

例如 c_communications.js

var comms = angular.module('my_app.c_comms',[])

comms.controller('AppComms',['$scope',function($scope){

    …

}])

此代码运行正常,但如果我添加另一个控制器 (c_communicationsB.js):

var commsb = angular.module('my_app.c_commsb',[])

并将 my_app.commsb 添加到 controllers.js

它给了我错误:

ionic.bundle.js:8895 Uncaught Error: [$injector:modulerr] Failed to instantiate module MY_APP due to:
Error: [$injector:modulerr] Failed to instantiate module my_app.controllers due to:
Error: [$injector:modulerr] Failed to instantiate module my_app.c_commsb due to:
Error: [$injector:nomod] Module ‘my_app.c_commsb' 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.4.3/$injector/nomod?p0=MY_APP.c_commsb
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:8895:12
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:10785:17
    at ensure (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:10709:38)
    at module (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:10783:14)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13189:22
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13190:40
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=MY_APP.c_commsb&…3A63342%2FSD4v040t2%2Fwww%2Flib%2Fionic%2Fjs%2Fionic.bundle.js%3A13173%3A5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:8895:12
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13212:15
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13190:40
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13190:40
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=MY_APP.controlle…3A63342%2FSD4v040t2%2Fwww%2Flib%2Fionic%2Fjs%2Fionic.bundle.js%3A13173%3A5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:8895:12
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13212:15
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
    at http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13190:40
    at forEach (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:9163:20)
    at loadModules (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13173:5)
    at createInjector (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:13099:11)
    at doBootstrap (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:10457:20)
    at bootstrap (http://localhost:63342/SD4v040t2/www/lib/ionic/js/ionic.bundle.js:10478:12)
http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=MY_APP&p1=Error%…A63342%2FSD4v040t2%2Fwww%2Flib%2Fionic%2Fjs%2Fionic.bundle.js%3A10478%3A12)

这里发生了什么?!

【问题讨论】:

  • communicationsB.js 和 my_app.commsb 的 cmets 用于显示什么是新代码,但是当我收到错误时没有注释,如果理解有误,请见谅

标签: javascript angularjs model-view-controller controller ionic


【解决方案1】:

解决了,

一周前,我使用了一个脚本来防止从浏览器中的缓存加载页面,以便在我在 chrome 上调试时无需刷新站点并强制始终从源代码读取。

我不知道是谁或为什么阻止在我添加的新 html 页面中加载新控制器或新 css,这就是让我想到这个脚本的原因。

【讨论】:

    【解决方案2】:

    //在controllers.js中

    angular.module('my_app',[
        //'ionic',
        'my_app.c_splash',
        'my_app.c_clock',
        'my_app.c_language',
        'my_app.c_comms',
        //'my_app.c_commsb',
        'pascalprecht.translate',
        'my_app.c_menu'
    ]);
    

    【讨论】:

    • 我从 app.js 调用 my_app.controller,所以我不能从这里擦除控制器,我试过了,它什么也没有加载,但没有错误
    • 您是否以正确的顺序加载它们?如果您尚未加载此控制器首先依赖的模块,那么它将失败
    【解决方案3】:

    您的应用似乎没有被调用。尝试将您的应用包装在 IFFY 中。

    (function () {
        var comms = angular.module('my_app.c_comms',[]);
    
        comms.controller('AppComms',['$scope',function($scope){
            //controller magic here
        }])
    
    }());
    

    这是一个自调用函数,因此您的应用会被自己调用。

    【讨论】:

    • 但我所有的其他控制器都没有包含在 IFFY 函数中,我不想使用它们
    【解决方案4】:

    你已经注释掉了&lt;!--&lt;script src="js/controllers/c_communicationsB.js"&gt;&lt;/script&gt;这一行

    在您的代码示例中,但已经说过该方法在此处。

    【讨论】:

    • 是的,当我遇到错误时,它没有评论,cmets。你可以在 controllers.js 中看到也被注释了,我这样做是为了解释代码的工作原理
    猜你喜欢
    • 2013-07-28
    • 2012-07-24
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    相关资源
    最近更新 更多