Return to site

ANGULAR INTERVIEW QUESTION: What is the difference between an Angular component and a directive?

August 25, 2021

A component is a class with the @Component decorator whose job is to create a view with a specific behavior, which will be added to the DOM elements hierarchy of the Angular app GUI.๐ŸŽจ

A directive is a class with the @Directive decorator. It does not has view (no HTML template), and only add a specific behavior to the DOM or to a component instance (in order to reuse it in multiple locations).๐Ÿ”ง

 

So you create a component when you want to add an element to the GUI DOM.

And you do a directive when you want to define a reusable behavior, to strengthen the already existing elements of your DOM.

 

๐“๐จ ๐ฌ๐ฎ๐ฆ-๐ฎ๐ฉ:

๐‚๐จ๐ฆ๐ฉ๐จ๐ง๐ž๐ง๐ญ = ๐•๐ข๐ž๐ฐ + ๐๐ž๐ก๐š๐ฏ๐ข๐จ๐ซ

๐ƒ๐ข๐ซ๐ž๐œ๐ญ๐ข๐ฏ๐ž = ๐‘๐ž๐ฎ๐ฌ๐š๐›๐ฅ๐ž ๐๐ž๐ก๐š๐ฏ๐ข๐จ๐ซ