Skip to content

Instantly share code, notes, and snippets.

@antho1404
Created January 8, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antho1404/7f51b9c3b260bc778dd4 to your computer and use it in GitHub Desktop.
Save antho1404/7f51b9c3b260bc778dd4 to your computer and use it in GitHub Desktop.
Debug your angular scopes and modules
findScope = (id, scope) ->
return null unless scope
return scope if "##{scope.$id}" is id
child = scope.$$childHead
while child
scope = findScope id, child
return scope if scope
child = child.$$nextSibling
return null
# return a scope object
# element: can be a DOM element (querySelector('...')) or the id of the scope
window.scope = (element) ->
if angular.isString(element)
findScope element, scope(document.body)
else
angular.element(element).scope()
# return the module (like resource, factory, service...) matching the name in parameter
# resource: is the name of the module, for example '$http'
window.getModule = (resource) ->
angular.element(document.body).injector().get resource
# display in console the tree view of the scope and it's children
# display for each scope an array of all the models included and the list of all the functions
window.inspectScope = (currentScope) ->
return unless currentScope
console.groupCollapsed "scope ##{currentScope.$id}"
models = scopeModels currentScope
functions = scopeFunctions currentScope
if Object.keys(models).length
table = []
table.push([key, angular.toJson(value)]) for key, value of models
console.table table
if Object.keys(functions).length
console.groupCollapsed "functions"
console.log "#{key}:", value for key, value of functions
console.groupEnd()
child = currentScope.$$childHead
while child
inspectScope child
child = child.$$nextSibling
console.groupEnd()
# return the list of models included in a scope given in parameter
window.scopeModels = (scope) ->
res = {}
for key in Object.keys(scope) when not key.match(/^\$/) and key isnt 'this'
value = scope[key]
res[key] = value if not angular.isFunction value
res
# return the list of functions included in a scope given in parameter
window.scopeFunctions = (scope) ->
res = {}
for key in Object.keys(scope) when not key.match(/^\$/) and key isnt 'this'
value = scope[key]
res[key] = value if angular.isFunction value
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment