Harke Harke - 25 days ago 11
AngularJS Question

When using custom directive, in which cases would it be better to use attribute instead of element type

From the angularJS docs, it's written that the common case for using element directive


is when you are creating a Domain-Specific Language for parts of your template. Use an attribute when you are decorating an existing element with new functionality.


The documentation gives an example for the element type but not for the attribute type. What does decorating an existing element with new functionality mean? Can someone provide an example?

UPDATE

I know that syntax for writing a directive.


The example given in the doc for element is as follows


Using an element for the myCustomer directive is clearly the right choice because you're not decorating an element with some "customer" behavior; you're defining the core behavior of the element as a customer component.


I am looking for a similar example, but for attribute. An example that explains the case when using the custom directive as attribute would be useful.

Answer Source

I got the answer I want. The links provide by @Mikko, link2 and link3 helped.

Those links give enough information to explain what decorating an existing element with new functionality mean. For eg: adding validation behavior to an input element.