Welcome to the ultimate guide to building custom directives in Angular! 🎉 If you are new to the topic or want to deepen your knowledge, you’ve come to the right place! 🤓

What Are Directives?

Directives are a powerful feature in Angular that allow you to extend the HTML syntax with new elements and attributes. They provide a way to encapsulate and reuse functionality in your application. With directives, you can create custom tags, attributes, and classes that can be used to manipulate the DOM, modify the behavior of elements, and much more. 💪

Types of Directives

There are three types of directives in Angular:

  • Component Directives: These are directives that create new components, which are self-contained units of UI. Components are the building blocks of Angular applications and can be visually rendered on the screen.

  • Attribute Directives: These are directives that change the appearance or behavior of an existing element, component, or directive. They can add, delete, or modify attributes of an element.

  • Structural Directives: These are directives that change the structure of the HTML by adding or removing elements. They manipulate the DOM by adding or removing a host element or its children.

Each type of directive has its own distinct use case and can be used to accomplish different goals. Understanding the different types of directives is critical for building custom directives in Angular. 🤔

Creating Custom Directives

To create a custom directive in Angular, we use the @Directive decorator, which allows us to configure the directive and specify where it can be used in the DOM. To create a custom attribute directive, we use the @Directive decorator with the selector property set to the name of the directive.

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
  selector: '[myDirective]'
})
export class MyDirective {
  @Input('myDirective') myValue: string;

  constructor(private el: ElementRef) { }

  ngOnInit() {
    console.log(this.myValue);
  }
}

With this code, we have created a custom directive called MyDirective. The @Input decorator allows us to pass a value to the directive from the template. In the ngOnInit function, we can access the value passed to the directive with the myValue input property. This directive can now be used in the template as an attribute on an element with the syntax myDirective="some value". 😎

Using Directives in Templates

Once we have created a custom directive, we can use it in the template as an attribute on an element. To do this, we add the attribute to the element and bind it to a value using property binding or interpolation.

<div myDirective="value"></div>

We can also use the directive with conditionals and loops using structural directives. For example, *ngIf and *ngFor are commonly used in templates to add or remove elements from the DOM based on a condition or an array.

<div *ngIf="showDiv">
  <p>Displayed only if showDiv is true</p>
</div>

<ul>
    <li *ngFor="let item of items"></li>
</ul>

Customizing Directive Behavior

We can customize directive behavior by passing options to the directive using binding or dependency injection. We can also use the @HostListener and @HostBinding decorators to listen to events on the element or to bind values to element properties.

import { Directive, HostListener, HostBinding } from '@angular/core';

@Directive({
  selector: '[myDirective]'
})
export class MyDirective {
  @HostBinding('style.backgroundColor') backgroundColor: string;

  @HostListener('mouseenter') onMouseEnter() {
    this.setBackgroundColor('blue');
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.setBackgroundColor(null);
  }

  constructor() { }

  setBackgroundColor(color: string) {
    this.backgroundColor = color;
  }
}

With this code, we have added the @HostBinding decorator to bind the value of the backgroundColor property to the style.backgroundColor of the host element. We have also added the @HostListener decorator to listen for the mouseenter and mouseleave events on the host element and set the background color accordingly. 🌈

Conclusion

In conclusion, creating custom directives in Angular can seem intimidating at first, but with practice, it becomes easier and more natural. Understanding the different types of directives, how to use them in templates, and how to customize their behavior are all crucial for building effective and maintainable Angular applications. So get coding and start building amazing Angular directives today! 🚀

A picture of a computer with Angular code on the screen.

Image Source: https://www.pexels.com/photo/code-coding-computer-data-developing-576834/