Skip to content

Instantly share code, notes, and snippets.

@everdimension
Last active February 15, 2016 12:27
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 everdimension/eb6fda0d2b6ec885f573 to your computer and use it in GitHub Desktop.
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.
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