A directive is instructions or guidelines for rendering a template. A class decorated with @Directive to attached metadata is called a directive. There are three types of directive supported by Angular, namely Component Directive, Structural Directive, and Attribute Directive:
A component is one form of a directive with a template that is decorated with @Component: it is actually an extended @Directive with a template feature:
<book-detail></book-detail>
Structural Directives manipulate the DOM elements and alter their structure by adding, removing, and replacing DOM elements. The following code snippet uses two Structural Directives:
<ul>
<li *ngFor="let book of books">
{{book.title}}
</li>
</ul>
Here, the div element has a *ngFor directive that iterates through the books collection object and replaces the title of each book.
An Attribute Directive helps to update the behavior or the appearance of an element. Let's use the Attribute Directive to set the font size of a paragraph. The following code snippet shows an HTML statement implemented with an Attribute Directive:
<p [myFontsize]>Fontsize is sixteen</p>
We need to implement a class annotated with @Directive along with the selector for the directive. This class should be implemented with the instructions on the behavior of the directive: