2

Assuming that a Custom Directive:

  • define a custom tag or attribute that is expanded or replaced
  • can include Controller logic, if needed

and that the Directive can be of type Element

<my-customdirective></my-customdirective>

or of type Attribute

<h4 my-customdirective></h4>

my question is WHEN and WHY I decide to use one rather than another?

Davide
  • 131
  • 5
  • 18

2 Answers2

4

Quoted from the documentation:

When should I use an attribute versus an element? Use an element when you are creating a component that is in control of the template. The common case for this 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.

Puigcerber
  • 9,814
  • 6
  • 40
  • 51
1

Some points to consider:

  • Most of the time attributes is the best/most convenient option (it's not the default by chance).
  • Anything you can do with element-bound directives, you can do with attribute-bound as well.
  • Element-bound directives can be more descriptive/readable at times.
  • If you want your code to pass certain types of validation, you should use attributes.
  • Since you want to support IE8, keep in mind that custom tags have an extra overhead (more info), which hurts maintainability.

For More Details

Priyank
  • 3,778
  • 3
  • 29
  • 48