What is Directives?

The @Directive decorator is a TypeScript decorator provided by Angular that is used to create custom directives. Directives in Angular are classes decorated with @Directive that defined behavior and logic to be applied to HTML elements in the DOM. These directives can manipulate the appearance, behavior, or structure of elements based on certain conditions or user interactions.

Syntax:

import { Directive } from '@angular/core';

@Directive({
selector: '[appBasicDirective]',
})
export class BasicDirective {
constructor() {}
}

Purpose of the @Directive decorator.

In Angular, Directives are defined as classes that can add new behavior to the elements in the template or modify existing behavior. The `@Directive` decorator in Angular is used to create a directive, which is a class that allows us to attach behavior to elements in the DOM. Directives are a way to extend the functionality of HTML elements and attributes in Angular applications.

Prerequisites:

Similar Reads

What is Directives?

The @Directive decorator is a TypeScript decorator provided by Angular that is used to create custom directives. Directives in Angular are classes decorated with @Directive that defined behavior and logic to be applied to HTML elements in the DOM. These directives can manipulate the appearance, behavior, or structure of elements based on certain conditions or user interactions....

Features of Directives

Reusability: Directives combine specific functionalities that allows you to reuse them across different parts of the application. This promotes code maintainability and reduces duplication.Attribute Modification: Attribute directives can dynamically add, remove, or modify attributes of HTML elements based on data or expressions. This allows for flexible styling and behavior control.Template Manipulation: Structural directives like *ngIf and *ngFor manipulate the template itself, that allows to conditionally display or iterate over elements based on data.Encapsulation: Directives encapsulate logic and behaviour within a class, that provides separation of concerns and making our code more maintainable.Access to Host Elements : Directives have access to host elements they are applied to, allowing them to interact with the element and its properties....

Types of Directives

There are two main categories of directives, each serving different purposes:...

Purpose of Directives

Directives in Angular extend HTML’s capabilities and allow us to manipulate the DOM.Directives help us to create reusable components and enhance code re usability.Customising element behaviour is possible using Angular directives.Directives enhance the re usability of code by encapsulating functionality.Attribute directives modify element behaviour, while structural directives alter the DOM’s structure....

Steps to create the Directive

Step 1: Create the angular project using the following command....