Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the attribute prefixes "x-" and "data-" used in AngularJS

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?

like image 497
sthomps Avatar asked Mar 06 '13 19:03

sthomps


People also ask

How are AngularJS prefixes and $$ used?

How AngularJS prefixes $ and $$ are used? The $ in AngularJs is a built-in object.It contains application data and methods. The scope($) acts as a link between controller and view. Inside the controller function the properties and the methods to the scope($) can be attached.

Which prefix is used with the AngularJS directives?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application.

What is angular attribute?

Angular attribute directives are a number of built-in directives that we can add to our HTML elements that give them a dynamic behavior. In summary, an attribute directive changes the appearance or behavior of a DOM element.

Which directive is used to define the variable in AngularJS?

ng-init: The ng-init directive is used to initialize an AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.


1 Answers

The HTML5 spec allows for arbitrary attributes as long as they're prefixed with data- like so:

<div data-myattribute=""></div> 

Whereas this would be invalid HTML5:

<div myattrbute=""></div> 

For more information on data- attributes, have a look here.

As for "x-" attributes, I think you mean "x:" attributes and elements, which are specific to XHTML validation...

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> 

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.

*behavior in browsers is going to vary with this xmlns approach.

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.

like image 78
Ben Lesh Avatar answered Sep 16 '22 11:09

Ben Lesh