How to debug Angular JavaScript Code
Solution 1:
1. Chrome
For debugging AngularJS in Chrome you can use AngularJS Batarang. (From recent reviews on the plugin it seems like AngularJS Batarang is no longer being maintained. Tested in various versions of Chrome and it does not work.)
Here is the the link for a description and demo: Introduction of Angular JS Batarang
Download Chrome plugin from here: Chrome plugin for debugging AngularJS
You can also use ng-inspect for debugging angular.
2. Firefox
For Firefox with the help of Firebug you can debug the code.
Also use this Firefox Add-Ons: AngScope: Add-ons for Firefox (Not official extension by AngularJS Team)
3. Debugging AngularJS
Check the Link: Debugging AngularJS
Solution 2:
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.
Solution 3:
there is also $log that you can use! it makes use of your console in a way that you want it to work!
showing the error/warning/info the way your console shows you normally!
use this > Document
Solution 4:
Despite the question is answered, it could be interesting to take a look at ng-inspector