What is the difference between a Component and a Directive?

Directive:

  • Directives help in modifying the behavior of an existing DOM element or adding new behavior to the DOM.
  • Directives are created by using the @Directive() decorator, and can be structural (which modify the DOM structure - ngIf, ngFor or ngSwitchCase) or behavioral (add new behavior to the existing DOM elements - ngStyle, ngClass or ngModel)
  • Directives don't possess their own View

Component:

  • Components are considered as a special branch of directives which can control a small portion of the View
  • Components are associated with an encapsulated View Template, Style and the View Logic (class) parts
  • These are created by using the @Component() decorator

Directive:

  • Directives help in modifying the behavior of an existing DOM element or adding new behavior to the DOM.
  • Directives are created by using the @Directive() decorator, and can be structural (which modify the DOM structure – ngIf, ngFor or ngSwitchCase) or behavioral (add new behavior to the existing DOM elements – ngStyle, ngClass or ngModel)
  • Directives don’t possess their own View

Component:

  • Components are considered as a special branch of directives which can control a small portion of the View
  • Components are associated with an encapsulated View Template, Style and the View Logic (class) parts
  • These are created by using the @Component() decorator

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *