Created
January 8, 2015 14:43
-
-
Save antho1404/7f51b9c3b260bc778dd4 to your computer and use it in GitHub Desktop.
Debug your angular scopes and modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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