28

Sometimes I see data-ng-controller but more often ng-controller

The ng-controller I already have used and this seems clear. But data-ng-controller.. what is for?

What are the differences between them, and where to use what?

ses
  • 13,174
  • 31
  • 123
  • 226

4 Answers4

31

There is no difference except for validation and browser compatibility.

Angular js will not work in IE8 unless data is present

[Update]

  • The above is for versions 1.0.* and 1.2.*
  • 1.3.* does not support ie8 so you don't need data-
FreshRob
  • 363
  • 1
  • 3
  • 9
13

Angularjs uses a normalization process for directive name / attributes matching.

From the angularjs docu at http://docs.angularjs.org/guide/directive.

Section Matching Directives:

The normalization process is as follows:

Strip x- and data- from the front of the element/attributes. Convert the :, -, or _-delimited name to camelCase.

bekite
  • 3,444
  • 2
  • 23
  • 31
  • 2
    Just a bit further down in the docs: Best Practice: Prefer using the dash-delimited format (e.g. ng-bind for ngBind). If you want to use an HTML validating tool, you can instead use the data-prefixed version (e.g. data-ng-bind for ngBind). – reergymerej Jan 30 '14 at 19:47
11

There's no difference between the two, except that prefixing with data- will allow the HTML to pass validation.

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

You may also see x-ng-controller. Both data- and x- are html5 custom attribute naming compliant.

Ioannis Suarez
  • 587
  • 6
  • 5