Angular Directives Created: 16 Jan 2026 Updated: 16 Jan 2026

Angular Directives : A Hands-On Guide

Angular directives are powerful tools that allow you to extend the behavior or appearance of standard HTML elements. While many directives use complex logic or data inputs, some of the most effective directives are those that automatically apply a specific state or style just by being present on an element.

In this article, we will build a simple directive called appFeatured. Its goal is to automatically apply a "featured" CSS class to any element it is attached to, using the safe and professional Renderer2 approach.

Understanding Renderer2: The Professional Bridge

When manipulating the DOM in Angular, you might be tempted to use ElementRef to access the nativeElement directly (e.g., element.classList.add()). However, this is generally discouraged in professional development. Instead, we use Renderer2.

Renderer2 acts as an abstraction layer between your code and the actual DOM.

  1. Platform Independence: Angular can run in environments where a standard browser DOM doesn't exist, such as Server-Side Rendering (SSR) or Web Workers. Direct DOM access will crash in these environments, but Renderer2 will handle them safely.
  2. Security: It provides a safer way to manipulate the UI, reducing the risk of XSS (Cross-Site Scripting) attacks.
  3. Best Practice: It ensures your code remains decoupled from the browser, making your application more robust and easier to test.

Step 1: Define the Styling in CSS

First, we define our visual "featured" style in the global CSS file (e.g., styles.css). This keeps our design logic where it belongs—in the stylesheet.

styles.css

/* The style applied automatically by our directive */
.featured-highlight {
border: 2px solid #ffd700; /* Gold border */
background-color: #fffdf0;
padding: 1rem;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(255, 215, 0, 0.2);
transition: transform 0.2s ease-in-out;
}

Step 2: Create the Directive

We will create the directive and use the ngOnInit lifecycle hook to apply the class immediately when the element is loaded into the DOM. Notice that we are not using @Input, making the usage as simple as possible.

featured.directive.ts

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

@Directive({
selector: '[appFeatured]', // Attribute selector used in HTML
standalone: true
})
export class FeaturedDirective implements OnInit {

constructor(
private el: ElementRef,
private renderer: Renderer2 // Injecting the safe renderer service
) {}

ngOnInit() {
// We tell the renderer to add our CSS class to the host element safely
this.renderer.addClass(this.el.nativeElement, 'featured-highlight');
}
}

Step 3: Implementation in the Template

To use the directive, simply import it into your component and add the appFeatured attribute to any tag.

app.component.html

<div class="container">
<h2>Our Exclusive Offers</h2>

<div appFeatured>
<h3>Premium Membership</h3>
<p>Get access to all exclusive content and 24/7 support.</p>
</div>

<div>
<h3>Basic Plan</h3>
<p>Start your journey with our standard features.</p>
</div>
</div>

Why This Matters

By using this simple directive approach, you achieve:

  1. Cleaner Templates: Your HTML remains semantic and readable. Instead of a long list of CSS classes, the attribute appFeatured clearly describes the intent of the element.
  2. Maintainability: If you decide that "featured" items should also have an icon or an extra attribute, you only need to update the directive in one place.
  3. Future-Proofing: Using Renderer2 ensures your app is ready for advanced Angular features like pre-rendering and SSR without any extra effort.


Share this lesson: