<head>的使用方法带有 Angular 指令的标签 [英] How to use &lt;head&gt; tag with Angular directive

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

问题描述

我在我的 Angular 应用程序中尝试了这个,但它不起作用.因此,我尝试将自定义标记()插入头部,并通过将head"替换为mytag"来使指令与此配合使用.

然而,这并不是我真正想要的,因为它在正文中添加了 而不是

有谁知道如何使它与头部标签一起工作?

解决方案

我遇到了同样的挑战.确保您的 angular 应用程序在 html 标签上初始化.然后这个解决方案开箱即用.

但是对于我们来说,这并不是一个理想的解决方案.所以我修改了 Zack Boman (tennisgent) https://github.com/tennisgent/angular-route-styles 代码,以便它可以在应用程序初始化后的任何地方使用.

  • 将指令重命名为:zbRouteStyles
  • 修改了限制以包含属性:restrict: 'EA'
  • 改变了这一行:elem.append($compile(html)(scope));angular.element('head').append($compile(html)(scope));

通过这些更改,我能够在我的 angular 应用程序初始化后将指令添加到任何标签,甚至是我的应用程序初始化的标签.

例如:

<div>

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?

解决方案

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) https://github.com/tennisgent/angular-route-styles 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)); 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.

e.g.:

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

这篇关于<head>的使用方法带有 Angular 指令的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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