Created
July 23, 2014 21:10
-
-
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
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
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