AngularJS 指令中自定义 HTML 标签的后果 [英] Consequences of Custom HTML Tags in AngularJS Directives

查看:19
本文介绍了AngularJS 指令中自定义 HTML 标签的后果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 AngularJS 中编写了一个自定义 gravatar 指令,该指令绑定到作用域上的 email 属性.该指令将替换此 HTML ...

Say I wrote a custom gravatar directive in AngularJS that is bound to an email property on the scope. The directive would replace this HTML …

<gravatar email="user.email" />

通过一个普通的 img 标签,其 src 属性被设置为正确的 Gravatar url:

by a common img tag whose src attribute is set to the correct Gravatar url:

<img src="http://gravatar.com/avatar/..." />

我的问题可能有点宽泛甚至幼稚,但是最初在我的页面的 HTML 中包含 标签有多糟糕"?除了没有通过 W3C 验证之外,还有其他现实世界"的后果吗?

My question might be a little broad or even naïve, but "how bad" is it to initially have the <gravatar /> tag in my page's HTML? Are there any "real-world" consequences besides not passing W3C validation?

推荐答案

W3C 说

作者不得将元素、属性或属性值用于其适当的预期语义目的以外的目的,因为这样做会阻止软件正确处理页面.

Authors must not use elements, attributes, or attribute values for purposes other than their appropriate intended semantic purpose, as doing so prevents software from correctly processing the page.

作者不得使用本规范或 其他适用规范,因为这样做会大大增加语言在未来的扩展难度.

Authors must not use elements, attributes, or attribute values that are not permitted by this specification or other applicable specifications, as doing so makes it significantly harder for the language to be extended in the future.

http://www.w3.org/html/wg/drafts/html/master/dom.html#elements

自定义元素/属性没有语义价值,因此应避免使用.

Custom elements/attributes have no semantic value and should be avoided for these reasons.

您可能不会注意到任何直接后果.毕竟,Angular 本身使用非确认的 ng 属性(尽管它们也支持 data- 前缀).

There probably won't be any direct consequences that you will even notice. After all, Angular itself uses the non-confirming ng attributes (although they do support with the data- prefix as well).

您可能面临的唯一可能问题是,您引入的当前是自定义元素的元素是否成为规范的一部分,并且具有与您之前预期不同的行为,但我认为这是极不可能的.即便如此,如果可以的话,我还是会避免使用任何自定义的东西.

The only possible problem you may face is if the element you introduce that is currently a custom element becomes part of the spec and has different behavior than what you were expecting before, but I think that is highly unlikely. Even so, I would avoid using anything custom if I could.

最终您将能够注册您的自己的自定义元素,但据我所知,目前尚无浏览器支持此规范.

Eventually you will be able to register your own custom elements, but from what I can tell no browser supports this spec yet.

这篇关于AngularJS 指令中自定义 HTML 标签的后果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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