如何使用< HEAD>与角指令标签 [英] How to use <head> tag with Angular directive

查看:154
本文介绍了如何使用< HEAD>与角指令标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过在我的角度应用程序,但它不工作。所以,我试图插入自定义标签(< MyTag的> )。入目,并通过与MyTag的取代头做出这个指令工作。

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".

不过,这是不是真的是我的本意,因为它增加了&LT; MyTag的&GT; 的身体,而不是&LT; HEAD&GT;

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?

推荐答案

我有同样的挑战。请确保您的应用角度上HTML标记初始化。那么这个解决方案开箱的。

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.

然而,对于我们这不是一个理想的解决方案。所以我修改扎克博曼(tennisgent)<一个href=\"https://github.com/tennisgent/angular-route-styles\">https://github.com/tennisgent/angular-route-styles code,这样就可以在任何地方应用初始化后使用。

However for us this was not an ideal solution. So I modified Zack Boman (tennisgent) https://github.com/tennisgent/angular-route-styles code, so that it could be used anywhere after app initialization.


  • 改名指令:zbRouteStyles

  • 修改了限制,包括属性:限制:EA

  • 改变该行: elem.append($编译(HTML)(范围));

    angular.element(头)追加($编译(HTML)(范围));

  • Renamed the directive to: zbRouteStyles
  • Modified the restrict to include attributes: restrict: 'EA'
  • Changed the line: elem.append($compile(html)(scope)); to angular.element('head').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.

例如:

&LT; D​​IV NG-应用=对myAppZB-路线的样式&GT;
&LT; D​​IV&GT;

这篇关于如何使用&lt; HEAD&GT;与角指令标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆