Skip to content

Instantly share code, notes, and snippets.

@dcadenas
Created July 23, 2014 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcadenas/566434ba0af9f3eabb07 to your computer and use it in GitHub Desktop.
Save dcadenas/566434ba0af9f3eabb07 to your computer and use it in GitHub Desktop.
A directive to deal with angular's problem when using $location and anchor links
myApp = angular.module('myApp')
#Ensures that:
# 1) all links contained in the directive's element that are not handled
# by angular do a full page reload bypassing html5mode.
# 2) all anchor links are prefixed with the current url so that the base
# path doesn't interfere
myApp.directive 'externalLinks', ->
link: (scope, element, attrs, controller) ->
element
.find("a:not([ng-click])")
.attr("target", "_self")
loc = window.location
urlWithoutHash = "#{loc.protocol}//#{loc.host}#{loc.pathname}"
element
.find("a[href^='#']")
.attr 'href', ->
urlWithoutHash + @hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment