How to debug Angular JavaScript Code

前端 未结 13 1985
离开以前
离开以前 2020-12-04 06:48

I am working on a proof of concept using Angular JavaScript.

How to debug the Angular JavaScript code in different browsers (Firefox and Chrome) ?

相关标签:
13条回答
  • 2020-12-04 06:56

    Try ng-inspector. Download the add-on for Firefox from the website http://ng-inspector.org/. It is not available on the Firefox add on menu.

    http://ng-inspector.org/ - website

    http://ng-inspector.org/ng-inspector.xpi - Firefox Add-on

    0 讨论(0)
  • 2020-12-04 06:59

    IMHO, the most frustrating experience comes from getting / setting a value of a specific scope related to an visual element. I did a lot of breakpoints not only in my own code, but also in angular.js itself, but sometimes it is simply not the most effective way. Although the methods below are very powerful, they are definitely considered to be bad practice if you actually use in production code, so use them wisely!

    Get a reference in console from a visual element

    In many non-IE browsers, you can select an element by right clicking an element and clicking "Inspect Element". Alternatively you can also click on any element in Elements tab in Chrome, for example. The latest selected element will be stored in variable $0 in console.

    Get a scope linked to an element

    Depending on whether there exists a directive that creates an isolate scope, you can retrieve the scope by angular.element($0).scope() or angular.element($0).isolateScope() (use $($0).scope() if $ is enabled). This is exactly what you get when you are using the latest version of Batarang. If you are changing the value directly, remember to use scope.$digest() to reflect the changes on UI.

    $eval is evil

    Not necessarily for debugging. scope.$eval(expression) is very handy when you want to quickly check whether an expression has the expected value.

    The missing prototype members of scope

    The difference between scope.bla and scope.$eval('bla') is the former does not consider the prototypically inherited values. Use the snippet below to get the whole picture (you cannot directly change the value, but you can use $eval anyway!)

    scopeCopy = function (scope) {
        var a = {}; 
        for (x in scope){ 
            if (scope.hasOwnProperty(x) && 
                x.substring(0,1) !== '$' && 
                x !== 'this') {
                a[x] = angular.copy(scope[x])
            }
        }
        return a
    };
    
    scopeEval = function (scope) {
        if (scope.$parent === null) {
            return hoho(scope)
        } else {
            return angular.extend({}, haha(scope.$parent), hoho(scope))
        }
    };
    

    Use it with scopeEval($($0).scope()).

    Where is my controller?

    Sometimes you may want to monitor the values in ngModel when you are writing a directive. Use $($0).controller('ngModel') and you will get to check the $formatters, $parsers, $modelValue, $viewValue $render and everything.

    0 讨论(0)
  • 2020-12-04 07:00

    Despite the question is answered, it could be interesting to take a look at ng-inspector

    0 讨论(0)
  • 2020-12-04 07:00

    Maybe you can use Angular Augury A Google Chrome Dev Tools extension for debugging Angular 2 and above applications.

    0 讨论(0)
  • 2020-12-04 07:01

    You can debug using browsers built in developer tools.

    1. open developer tools in browser and go to source tab.

    2. open the file do you want to debug using Ctrl+P and search file name

    3. add break point on a line ny clicking on left side of the code.

    4. refresh the page.

    There are lot of plugin available for debugging you can refer for using chrome plugin Debug Angular Application using "Debugger for chrome" plugin

    0 讨论(0)
  • 2020-12-04 07:07

    You can add 'debugger' in your code and reload the app, which puts the breakpoint there and you can 'step over' , or run.

    var service = {
    user_id: null,
    getCurrentUser: function() {
      debugger; // Set the debugger inside 
                // this function
      return service.user_id;
    }
    
    0 讨论(0)
提交回复
热议问题