Search code examples
angularjsangularjs-directiveangularjs-scopeangularjs-ng-repeat

What is the best way to debug angular code?


I'm a newbie in the Angular space. I've been having trouble debugging my code. Any methods proven more effective in debugging Angular?


Solution

  • open the developer console in the chrome browser, this allows you to:

    • set breakpoints and see whats going on in your code
    • use the console in the developer tools while you are waiting in breakpoints to execute some code or check the contents of variables
    • use the "select element" tool to select any HTML element, then in the console type "$scope" to see what's on the $scope of this element.

    general tipps:

    • use some console.log statment like "running ControllerXYZ now" so you see, when your controllers are executed or when you are transitioning between states.
    • When using ui-router I find it extremly important to add a console.log statement into $stateChangeStart, $stateChangeSuccess and $stateChangeError event handlers, that shows you when you transition between states or when errors happen on state transitions, without that I am blind...
    • adding some {{anImportantVariable | json}} into html often is enough to debug stuff.

    and the most important: if everything looks ok, and it still does not work, the problem is in 99%: