A Comprehensive Guide to Angular Directives: Building Powerful UI Components

Explore Angular directives, their types, and how to create them from scratch. Learn how to build powerful UI components with Angular.

Astrit Shuli
Bits and Pieces

--

A Comprehensive Guide to Angular Directives: Building Powerful UI Components. Astrit Shuli
A Comprehensive Guide to Angular Directives: Building Powerful UI Components. Astrit Shuli

Introduction

Angular is a popular and powerful JavaScript framework for building modern web applications. One of its key features is directives, which allow developers to extend HTML and build custom, reusable UI components. In this article, we will explore Angular directives, their types, and how to create them from scratch. By the end, you’ll be equipped with the knowledge to build sophisticated UI components for your Angular applications.

Table of Contents

  1. What are Angular Directives?
  2. Built-in Directives vs. Custom Directives
  3. Creating a Basic Custom Directive
  4. Directive Inputs and Outputs
  5. Directive Lifecycle Hooks
  6. Structural Directives
  7. Attribute Directives
  8. HostListener and HostBinding
  9. ngContent and Content Projection
  10. Conclusion

1. What are Angular Directives?

Angular directives are markers on a DOM element that tell Angular’s compiler to attach a specified behavior to that element or transform the element and its children. They allow us to extend HTML with new attributes and elements, creating reusable components.

💡 Note: If you find yourself in a situation where you’re having to use the same custom directives for multiple HTML templates, consider adding them to modules which you can then independently test, version, document, and finally reuse across multiple projects using a tool like Bit.

Learn more:

2. Built-in Directives vs. Custom Directives

Angular comes with built-in directives like ngIf, ngFor, and ngStyle, which are commonly used in Angular applications. Custom directives, on the other hand, are created by developers to meet specific requirements. We will focus on custom directives in this article.

3. Creating a Basic Custom Directive

Let’s start by creating a basic custom directive that highlights an element with a specified color. First, create a new file highlight.directive.ts:

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

@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() highlightColor: string = 'yellow';

constructor(private el: ElementRef, private renderer: Renderer2) {
this.highlight();
}

private highlight() {
this.renderer.setStyle(this.el.nativeElement, 'background-color', this.highlightColor);
}
}

4. Directive Inputs and Outputs

Directives can accept inputs and emit outputs. Inputs allow you to pass data into a directive from the outside, and outputs enable a directive to emit events to the parent component. Let’s enhance our highlight.directive.ts to accept a custom highlight color:

@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input('appHighlight') highlightColor: string = 'yellow';

constructor(private el: ElementRef, private renderer: Renderer2) {
this.highlight();
}

private highlight() {
this.renderer.setStyle(this.el.nativeElement, 'background-color', this.highlightColor);
}
}

5. Directive Lifecycle Hooks

Angular provides several lifecycle hooks for directives. They allow you to react to changes in the directive’s properties or execute code at specific points in its lifecycle. The most commonly used hooks are ngOnInit, ngOnChanges, and ngOnDestroy.

6. Structural Directives

Structural directives shape the DOM’s structure by adding, removing, or manipulating elements. The ngIf and ngFor directives are excellent examples of structural directives.

7. Attribute Directives

Attribute directives change the appearance or behavior of an element, component, or another directive. The ngStyle and ngClass directives are attribute directives.

8. HostListener and HostBinding

@HostListener and @HostBinding are decorators that allow you to listen to events on the host element and bind to the host element's properties, respectively.

9. ngContent and Content Projection

ngContent and content projection enable you to pass content into a component from the outside. They are useful when you want to create reusable components with dynamic content.

10. Conclusion

In this article, we explored the power of Angular directives, their types, and how to create custom directives from scratch. With this knowledge, you can now build powerful and reusable UI components for your Angular applications. Directives are a fundamental part of Angular development, and mastering them will significantly enhance your productivity as a developer.

Happy coding! 😊

Note: Make sure to include any code examples or Angular application setup instructions to help readers understand and implement the concepts effectively. Additionally, add relevant images or diagrams to enhance the article’s visual appeal and clarity.

Build composable Angular apps with reusable components, just like Lego

Bit is an open-source toolchain for the development of composable software.

With Bit, you can develop any piece of software — a modern web app, a UI component, a backend service or a CLI script — as an independent, reusable and composable unit of software. Share any component across your applications to make it easier to collaborate and faster to build.

Join the 100,000+ developers building composable software together.

Get started with these tutorials:

→ Micro-Frontends: Video // Guide

→ Code Sharing: Video // Guide

→ Modernization: Video // Guide

→ Monorepo: Video // Guide

→ Microservices: Video // Guide

→ Design System: Video // Guide

--

--