AngularJS 应用是否应该 HTML5 兼容?

Should AngularJS apps be HTML5-compliant?

我正在熟悉 AngularJS,并且我注意到可以通过在指令前加上 data- 使您的模板代码兼容 HTML5。 (例如,data-ng-repeat="..." 而不是 ng-repeat="..."。)

我的第一直觉是将我的所有指令都这样作为前缀,但我一直在想:这样做是否有令人信服的理由?是否有任何确认的浏览器因无效 HTML 属性而发生故障的案例?一段时间后,在所有内容前面键入 data- 会变得非常乏味,这会使 HTML 模板的可读性大大降低。

Attribute names must consist of one or more characters other than the space characters, U+0000 NULL, U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('), ">" (U+003E), "/" (U+002F), and "=" (U+003D) characters, the control characters, and any characters that are not defined by Unicode. In the HTML syntax, attribute names, even those for foreign elements, may be written with any mix of lower- and uppercase letters that are an ASCII case-insensitive match for the attribute's name.

http://www.w3.org/TR/html/syntax.html#attributes-0

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

http://www.w3.org/TR/html5/dom.html#custom-data-attribute

经过更多挖掘,我找到了 data-* 属性的部分。但是,它并没有真正说它是 "must"。更像是 "we recommend using it".

Afaik,浏览器无法识别的属性将被忽略,但仍可通过 DOM 对象的 getAttribute 方法和 attributes 属性 访问。 data-* 通常是语义和方便的首选,尤其是在使用 dataset.

类似地,旧版本的 angular 甚至 recommends attribute name restrict 指令作为元素名称的替代方法限制了 IE8 兼容性。指令通常有自定义名称,这应该是自定义属性名称很好的一个指标。