属性前缀“x-"如何?和“数据-"在 AngularJS 中使用 [英] How are the attribute prefixes "x-" and "data-" used in AngularJS
问题描述
我是 Angular 的新手,并试图理解x-"和data-"前缀的含义.在指令文档(http://docs.angularjs.org/guide/directive)中,它说这些前缀将使指令符合 HTML 验证器".这究竟是什么意思?
I'm new to Angular and trying to understand what the "x-" and "data-" prefixes mean. In the directives documentation (http://docs.angularjs.org/guide/directive) it says that these prefixes will make the directive "HTML validator compliant". What exactly does this mean?
推荐答案
HTML5 规范允许任意属性,只要它们以数据为前缀,就像这样:
The HTML5 spec allows for arbitrary attributes as long as they're prefixed with data- like so:
<div data-myattribute=""></div>
而这将是无效的 HTML5:
Whereas this would be invalid HTML5:
<div myattrbute=""></div>
至于x-"属性,我认为您的意思是x:"属性和元素,它们特定于 XHTML 验证...
As for "x-" attributes, I think you mean "x:" attributes and elements, which are specific to XHTML validation...
为了扩展这一点,如果您(出于某种原因)使用 XHTML,您可以像这样定义带有命名空间的自定义属性(我只是在这里总结要点):
To expand on this, if you were to (for some reason) be using XHTML, you can define custom attributes with namespacing like so (and I'm just summarizing the gist here):
<html xmlns:x="http://sample.com/mynamespace">
<body>
<div x:whatever=""></div>
<x:mytag></x:mytag>
</body>
</html>
xmlns 中的 URL 实际上只是为了防止相似元素之间的冲突.此外,自定义元素和属性的 DTD 可以作为 DOCTYPE 声明的一部分用于验证目的.
where the URL in xmlns is really just to prevent conflicts between like elements. Also, a DTD for the custom elements and attributes could be provided for validation purposes as a part of your DOCTYPE declaration.
*浏览器中的行为会因这种 xmlns 方法而异.
*behavior in browsers is going to vary with this xmlns approach.
总而言之:对于过去三年发布的大多数浏览器或 IE8+,您不必担心任何这些事情.只有在非常特殊的情况下,您才会真正地关心.
In summary, though: With most browsers released in the last three years, or IE8+ you're not going to have to worry about any of these things. Only in very specific situations will you really care.
这篇关于属性前缀“x-"如何?和“数据-"在 AngularJS 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!