Skip to content

Instantly share code, notes, and snippets.

@JasonStoltz
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save JasonStoltz/7d7f6755b7b6e035bb04 to your computer and use it in GitHub Desktop.

Select an option

Save JasonStoltz/7d7f6755b7b6e035bb04 to your computer and use it in GitHub Desktop.
Angular recursive scope find
(function(){
var root = angular.element(document.body).injector().get('$rootScope');
function children(scope) {
var arr = [];
var head = scope.$$childHead;
if (head) {
arr.push(head);
ir = head.$$nextSibling;
while(ir) {
arr.push(ir);
ir = ir.$$nextSibling;
}
}
return arr;
}
function go(scope, search) {
if (scope[search]) {
return [scope];
} else {
var childs = children(scope);
return _.flatten(
childs.map(
function(child){
return go(child, search);
}
)
);
}
}
window.fuzzySearch = _.partial(go, root);
window.fuzzySearchPlus = function(search) {
return window.fuzzySearch(search).map(function(s){
function getScope(id) {
var elem;
$('.ng-scope').each(function(){
var s = angular.element(this).scope(),
sid = s.$id;
if(sid == id) {
elem = this;
return false; // stop looking at the rest
}
});
return elem;
}
return {
element: getScope(s.$id),
scope: s,
value: s[search],
inspect: function(){
inspect(this.element);
}
}
})
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment