简体   繁体   English

如何调试这个角度js代码?

[英]How to debug this angular js code?

When errors happen inside of angular, debugging seems impossible for me. 当角度内部发生错误时,调试对我来说似乎是不可能的。 I have this code: 我有这个代码:

<script>
    var my_app = angular.module("campaign_listing", ['$http']);
    var controllers = {};
    controllers.campaign_list_controller = function($http){
        var filters = {};
        var campaigns = {};
        this.update_fields = function(){
            console.log("update!");
        }
    }
    my_app.controller(controllers);
</script>

And it throws this error: 它会抛出这个错误:

    angular.js:38Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.6/$injector/modulerr?p0=campaign_listing&p1…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.6%2Fangular.min.js%3A19%3A463)

And without the minified version:

Uncaught Error: [$injector:modulerr] Failed to instantiate module campaign_listing due to:
Error: [$injector:modulerr] Failed to instantiate module $http due to:
Error: [$injector:nomod] Module '$http' 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.6/$injector/nomod?p0=%24http

I'm not using routing, so that link actually confuses me. 我没有使用路由,所以链接实际上让我困惑。 Can someone tell me what the problem is and how to debbug this in the future? 有人能告诉我问题是什么以及如何在将来对此进行调查吗?

You could create a breakpoint in the line of my_app.controller() and step into the Angular code (good luck, that's hard for beginners). 你可以在my_app.controller()的行中创建一个断点并进入Angular代码(祝你好运,这对初学者来说很难)。

Only from reading your code, I suggest to try this: 只有阅读您的代码,我建议尝试这样做:

<script>
    var my_app = angular.module("campaign_listing", []);
    var campaign_list_controller = function($http){
        var filters = {};
        var campaigns = {};
        this.update_fields = function(){
            console.log("update!");
        }
    }
    my_app.controller('campaign_list_controller', campaign_list_controller);
</script>

I suppose you might get even more problems later, because this all doesn't look much like typical Angular code. 我想你以后可能会遇到更多问题,因为这看起来并不像典型的Angular代码。 My advise: try to exercise with the Tutorial first. 我的建议:首先尝试使用教程。

The previous answer is correct but this is can also happen when you do NOT include a Controller file. 上一个答案是正确的,但是当您不包含Controller文件时也会发生这种情况。 Example below shows at line 60 I had commented out the controller file but at line 13 I was actually referencing a AppCtrl. 下面的示例显示在第60行,我已经注释掉了控制器文件,但在第13行,我实际上引用了一个AppCtrl。 If I uncomment the line 60 the error goes away. 如果我取消注释第60行,则错误消失。

在此输入图像描述

Here is what you would get on console: 这是你在控制台上得到的: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM