Last active
February 15, 2016 12:27
-
-
Save everdimension/eb6fda0d2b6ec885f573 to your computer and use it in GitHub Desktop.
An angular directive making svg's work when <base href="/path"> tag is present.
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
angular.module('app.directives.svgBaseFix', []) | |
.directive('svgBaseFix', function($rootScope) { | |
return { | |
restrict: 'A', | |
link: function(scope, element, attrs) { | |
var attr = 'xlinkHref'; | |
var initialUrl = attrs[attr]; | |
var parsingNode = document.createElement('a'); | |
attrs.$observe(attr, updateValue); | |
$rootScope.$on('$locationChangeSuccess', updateValue); | |
function updateValue() { | |
var newVal; | |
parsingNode.setAttribute('href', location.pathname + location.search + initialUrl); | |
newVal = parsingNode.toString(); | |
if (newVal && attrs[attr] !== newVal) { | |
attrs.$set(attr, parsingNode.toString()); | |
} | |
} | |
} | |
}; | |
}); | |
// | |
// Usage | |
// | |
// <svg> | |
// <use svg-base-fix xlink:href="#icon-quote"></use> | |
// </svg> | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment