There is a similar question out there, but it only applies to hard-coded hrefs, not ones that are interpolated by angular. See Conditionally add target="_blank" to links with Angular JS
The solution I'm looking for is best illustrated by the following directive:
yourModule.directive('a', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
var a = elem[0];
if (location.host.indexOf(a.hostname) !== 0)
a.target = '_blank';
}
}
}
The problem is that the directive runs before angular has done any interpolation, and therefore all links appear relative. Is there a clean way to do this? I can't use mutation observers, because I must support IE9.