How does AngularJS get away with using custom HTML5 element tags and attributes?

前端 未结 4 662
故里飘歌
故里飘歌 2020-12-08 13:03

Most browsers seem to allow you to create any element tag you like, and add any attribute name you like to elements. For example:


<         


        
相关标签:
4条回答
  • 2020-12-08 13:40

    From W3C specification:

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

    However, while the initial source of the page body (before angular processes an ng-app element) may not adhere to the W3C standards, if you use replace: true in directives, custom elements are replaced by a template HTML, which can be valid. So, in this case, you can think about an angular element as just a placeholder that is replaced with the terminal HTML output.

    0 讨论(0)
  • 2020-12-08 13:48

    This is kinda opinionated, but so is the topic. The web is now what we make it, we have the ability to create/style/modify non-spec elements. And the Angular team may not really care about specs, its the cutting edge, they allow you to do what you want. That being said, you can do anything and everything in Angular with markup that will validate and conforms to the HTML5 spec (as in they support it both ways).

    0 讨论(0)
  • 2020-12-08 14:02

    AngularJS don't really care, it's totally up to you. If you want your directive to be compatible with olds IE, you need to use <div data-hello-world> instead of <hello-world> and put data- in front of all your custom attributes.

    HTML5 is still not 100% the norm, but it's slowly becoming the norm.

    0 讨论(0)
  • 2020-12-08 14:06

    It won't validate but it will render.

    HTML5 is designed to be very forgiving (contrary to XHTML). This is in part so that "older" HTML5 browsers can allow new elements (as well as badly formatted HTML) which that version doesn't support yet without breaking the page.

    This can of course be "exploited" to introduce custom tags.

    That being said - you can also do this with HTML4.

    As mentioned in comments: you also have the ability to prefix any attributes in an ordinary tag with data- and it will validate.

    0 讨论(0)
提交回复
热议问题