Last Updated: January 20, 2020
·
3.591K
· eldadfux

Valid Angular JS directives

When adding Angular JS directives in your DOM make sure to write them with a data- prefix.

This would be best practice as it will allow your HTML to be valid according to the W3C standards and W3C Validator (http://validator.w3.org/).

Creating your own elements attributes as show in many Angular JS sample code and application is not alongside with the HTML5 standards as it will make future extensions of the language impossible.

Bad Example:

<form ng-submit="addTodo()">
  <input type="text" ng-model="todoText"  size="30"
         placeholder="add new todo here">
  <input class="btn-primary" type="submit" value="add">
</form>

Good Example:

<form data-ng-submit="addTodo()">
  <input type="text" data-ng-model="todoText"  size="30"
         placeholder="add new todo here">
  <input class="btn-primary" type="submit" value="add">
</form>

2 Responses
Add your response

In this case you should use x-* prefix instead data-*.
data is for the other purpose.

over 1 year ago ·

I just asked this question on AngularJS community on Google+: what would be considered a better style, to use 'data-ng-' prefixes or 'x-ng-' prefixes.
http://goo.gl/0YqYrd

over 1 year ago ·