Last active
September 16, 2016 19:37
-
-
Save DarkArc/e7c9700588afa6b146f50107a941021a to your computer and use it in GitHub Desktop.
Search up through an angular scope to find the object holding a given named scope variable
This file contains 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
function findKey(searchObject, scopeVariableName) { | |
for (var objectKey in searchObject) { | |
if (objectKey === scopeVariableName) { | |
return searchObject; | |
} else if (objectKey === '$parent' && searchObject[objectKey] instanceof Object) { | |
if (searchObject.hasOwnProperty(objectKey)) { | |
var result = findKey(searchObject[objectKey], scopeVariableName); | |
if (result) { | |
return result; | |
} | |
} | |
} | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment