What are directives in Angular ?
In Angular, directives are one of the core building blocks used to extend HTML's capabilities by creating reusable components or altering the behavior of the DOM. Directives are special markers (usually attributes or elements) in the HTML code that Angular uses to add additional functionality or behavior to elements in the DOM (Document Object Model).
There are three main types of directives in Angular:
Component Directives
- A component is essentially a directive with a template. Technically, every Angular component is also a directive.
- Components are used to define views and manage the user interface in Angular. They are usually the most common type of directive.
- Example:
In this example,
UserProfileComponent
is a directive with an associated template (<h2>{{ user.name }}</h2>
).
Structural Directives
- Structural directives are responsible for changing the structure of the DOM by adding or removing elements. These directives modify the layout of the DOM by adding, removing, or manipulating elements based on conditions or loops.
- Structural directives typically start with an asterisk (
*
) in the template. - Common examples of structural directives:
*ngIf
: Conditionally includes an element in the DOM.*ngFor
: Loops through a list and creates a new element for each item.*ngSwitch
: Conditionally renders a set of elements based on a case or condition.
Example of
*ngIf
:If
isVisible
istrue
, the<div>
will be added to the DOM; if it'sfalse
, the<div>
will be removed.Example of
*ngFor
:This directive repeats the
<li>
element for each item in theitems
array.Attribute Directives
- Attribute directives are used to change the appearance or behavior of an existing element in the DOM. They don’t change the structure of the DOM but modify the element’s properties, styles, or add event listeners.
- These directives are applied as attributes to an HTML element.
- Examples of attribute directives include:
ngClass
: Adds or removes CSS classes from an element.ngStyle
: Dynamically sets styles on an element.ngModel
: Binds the value of an input element to a property in the component.
Example of
ngClass
:If
isHighlighted
istrue
, the classhighlighted
will be added to thediv
, otherwise, it won’t be added.Example of
ngStyle
:The color of the text will be dynamically bound to the
color
property in the component.
Key Points About Directives:
- Directives are used to extend HTML functionality by adding behavior to DOM elements.
- Components are a type of directive with templates, whereas other directives (structural and attribute) are used for modifying the DOM in different ways.
- Custom directives can be created to implement specific logic or functionality that is reusable across the application.
How Directives Work:
- Structural Directives (like
*ngIf
or*ngFor
) are applied to elements and can either create or remove elements from the DOM based on the conditions they are given. - Attribute Directives (like
ngClass
,ngStyle
) are applied to the properties or styles of DOM elements and can change their appearance or behavior dynamically.
Creating Custom Directives:
You can create custom directives in Angular to handle specific tasks that you need to reuse across your app. Here's a simple example of a custom attribute directive:
Example of a custom directive that changes the background color:
Create the directive:
Use the directive in your template:
In this example, the appHighlight
directive changes the background color of the element it's applied to.
Conclusion:
Directives in Angular are a powerful way to extend HTML and add custom functionality or behavior to DOM elements. There are built-in directives for structural (e.g., *ngIf
, *ngFor
) and attribute (e.g., ngClass
, ngStyle
) manipulations, and you can create your own custom directives to encapsulate reusable functionality. This helps keep Angular applications modular, clean, and maintainable.
Comments
Post a Comment