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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.