How to use <head> tag with Angular directive

All we need is an easy explanation of the problem, so here it is.

I tried this in my Angular app, but it does not work. So I tried inserting a custom tag(<mytag>) into the head and made the directive work with this by replacing “head” with “mytag”.

This however is not really what I intended, because it adds <mytag> to the body instead of the <head>

Does anyone know how to make it work with the head-tag?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I had the same challenge. Make sure that your angular app is initialized on the html tag. Then this solution works out of the box.

However for us this was not an ideal solution. So I modified Zack Boman (tennisgent) code, so that it could be used anywhere after app initialization.

  • Renamed the directive to: zbRouteStyles
  • Modified the restrict to include attributes: restrict: ‘EA’
  • Changed the line: elem.append($compile(html)(scope));

With these changes I was able to add the directive to any tag after my angular app was initialized even the tag that my app is initialized on.


<div ng-app="myApp" zb-Route-Styles>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply