DEV Community

Cover image for Directives In Angular
haimantika mitra for Angular

Posted on

7 1

Directives In Angular

Hi readers πŸ‘‹, welcome to the third part of the series, where we get introduced to Directives!

In the last two articles, we talked about Components and Data Bindings. If you are a beginner like me, we might want to go through them first and then hop on to this article.

What is Directives?

Directives are classes that add additional behavior to elements in your Angular applications. It creates DOM elements, change their structure, or behavior in an Angular
application.

There are three types of directives:

  • Components - It is the most common type of directive and is used with a template
  • Attribute directives - Changes the appearance or behavior of an element, component, or another directive
  • Structural directives - Change the DOM layout by adding and removing DOM elements

More Details

The major difference between component directives and attribute and structural directive is - component directive has a template, while the others do not.

  • Inbuilt structural directives - *ngIf, *ngFor
  • Inbuilt attribute directives - NgStyle, NgModel, NgClass

Attribute Directive

Attribute directives listen to and modify the behavior of other HTML elements, attributes, properties, and components.

Details of the most common attribute directive:

  • NgClass- Adds and removes a set of CSS classes.
  • NgStyle- Adds and removes a set of HTML styles.
  • NgModel- Adds two-way data binding to an HTML form element.

Example of creating a custom directive:

import { Directive, ElementRef, Renderer } from β€˜@
angular/core’;
@Directive({
selector: β€˜[appChbgcolor]’
})
export class ChangeBgColorDirective {
constructor(private el: ElementRef, private renderer:
Renderer) {
this.ChangeBgColor(β€˜red’);
}
ChangeBgColor(color: string) {
this.renderer.setElementStyle(this.l.nativeEleme
nt,β€˜color’, color);
}
}
Enter fullscreen mode Exit fullscreen mode

To create a custom attribute directive, you need to create a class and decorate it with @Directive.
In the constructor of the directive class, we inject the services ElementRef and Renderer. Instances of these two classes are needed to get the reference of the host element and of the renderer.

Structural Directives

Structural directives change the structure of the DOM elements, For eg. *ngIf is a structural directive, which provides an 'if' condition statements that are to be executed. If the condition becomes False, then the elements are removed from DOM. If the condition is True, then the elements are added to DOM. Whereas, *ngFor structure directive creates DOM elements in a loop.

Example of *ngIf:

@Component({
selector: β€˜app-notification’,
template:`
<div *ngIf = β€˜showNotif’>
Show Notification
</div>
`
})
export class AppNotificationComponent {
showNotif = true;
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the condition is true, thus the elements will be added to DOM.

Conclusion

From this article we see that Directives are an essential part of Angular, depending on our task, we can use the different types of Directives.

Thank you for reading till the end. Happy learning 🌻

For any questions or suggestions, drop them on the comment below or reach out to me @Haimantika.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay