Skip to content

Instantly share code, notes, and snippets.

@dancek
Last active December 27, 2015 16:29
Show Gist options
  • Save dancek/7355527 to your computer and use it in GitHub Desktop.
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.
# 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