I am working on a proof of concept using Angular JavaScript.
How to debug the Angular JavaScript code in different browsers (Firefox and Chrome) ?
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
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!
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.
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.
Not necessarily for debugging. scope.$eval(expression)
is very handy when you want to quickly check whether an expression has the expected value.
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())
.
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.
Despite the question is answered, it could be interesting to take a look at ng-inspector
Maybe you can use Angular Augury A Google Chrome Dev Tools extension for debugging Angular 2 and above applications.
You can debug using browsers built in developer tools.
open developer tools in browser and go to source tab.
open the file do you want to debug using Ctrl+P and search file name
add break point on a line ny clicking on left side of the code.
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
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;
}