Skip to content

Instantly share code, notes, and snippets.

@IlanFrumer
Last active August 29, 2015 13:55
Show Gist options
  • Save IlanFrumer/8764935 to your computer and use it in GitHub Desktop.
Save IlanFrumer/8764935 to your computer and use it in GitHub Desktop.
ui router - trailing slash
app.config ($urlRouterProvider)->
##############################################################################################################################
# https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes
##############################################################################################################################
$urlRouterProvider.rule ($injector, $location)->
path = $location.path()
# Note: misnomer. This returns a query object, not a search string
search = $location.search()
# check to see if the path already ends in '/'
return if path[path.length - 1] is "/"
# If there was no search string / query params, return with a `/`
return path + "/" if Object.keys(search).length is 0
# Otherwise build the search string and return a `/?` prefix
params = []
angular.forEach search, (v, k) ->
params.push k + "=" + v
path + "/?" + params.join("&")
$urlRouterProvider.otherwise('/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment