Last active
December 27, 2015 16:29
-
-
Save dancek/7355527 to your computer and use it in GitHub Desktop.
Easy scope value to location mapping with friendly names. Use this inside an angular controller.
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
# scope to url query part mapping ( #!/?key=val ) | |
queryMapping = | |
category: 'cat' | |
searchQuery: 'q' | |
'selected.id': 'show' | |
query = $location.search() | |
for key, queryKey of queryMapping | |
# initially set scope values to query params | |
if queryKey of query | |
$scope[key] = query[queryKey] | |
# add watchers | |
$scope.$watch key, do -> | |
_key = queryKey | |
(newValue, oldValue) -> | |
q = $location.search() | |
if newValue? and newValue != '' | |
q[_key] = newValue | |
else | |
delete q[_key] | |
$location.search(q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment