
Angular has always been my go-to framework for building scalable, enterprise-grade applications. And just when you think the template syntax couldnât get more powerfulâAngular introduces Selectorless Directives.
This feature is a huge step forward in how we build and compose behavior in Angular apps. In this post, Iâll break it down in a way thatâs practical, real-world, and easy to adopt.
â What Weâll Cover
- What selectorless directives actually are
- Why theyâre a big deal for Angular devs
- How to use them with real-life examples
- How they stack up against the old way
- The boost they give to DX (Developer Experience)
đ What Are Selectorless Directives?
Traditionally, Angular directives are attached using selectorsâeither as attributes, elements, or class-based hooks.
Selectorless directives flip the script: they let you apply directives like functions inside your HTML. No more app-*
prefixes or verbose wrappers.
đĄ Example
<button
@AdvancedButton(text="Click me")
@Tooltip(text="Click to expand")>
</button>
Notice how clean that looks? You’re enhancing the button directly without bloating your template with custom components or attribute clutter.
It’s declarative, readable, and modularâall in one.
đ§ Why This Matters
Selectorless directives solve some long-standing pain points in Angular templates:
â
No more input name conflicts
â
Apply multiple behaviors without selector clashes
â
Cleaner, more readable HTML
â
Compose behaviors like React Hooks or Vue ComposablesâAngular-style
This change makes your templates feel more like a mini-DSL for behavior. Itâs especially useful when building component libraries or dealing with complex UI logic.
đ§Ş Real-World Use Case: Enhancing a Button
Letâs say you want to apply several behaviors to a single button:
- Tooltip
- Analytics tracking
- Animation
- Role-based access control
Old Way
<button
appTooltip="Click to expand"
appTrackAnalytics
appButtonAnimation
appPermission="admin">
Click me
</button>
With Selectorless Directives
<button
@Tooltip(text="Click to expand")
@TrackAnalytics(event="click_button")
@Animate(effect="ripple")
@Permission(role="admin")>
Click me
</button>
Thatâs clean, expressive, and super easy to maintain.
đ Before vs. After
Feature | Traditional Directives | Selectorless Directives |
---|---|---|
Syntax | Verbose (app-* ) | Functional (@DirectiveName() ) |
Input conflicts | Risk of collisions | Isolated per directive |
Behavior composition | Tedious | Clean and stackable |
Readability | Cluttered templates | Declarative and focused |
âď¸ How It Works (Under the Hood)
Selectorless directives use Angular’s new functional directive API. Hereâs how to create one:
import { directive, input, effect } from '@angular/core';
export const Tooltip = directive({
inputs: {
text: input<string>(),
},
setup({ text }) {
effect(() => {
const tooltipText = text();
// Bind tooltip logic to host element
});
}
});
To apply it in the template, just use:
<button @Tooltip(text="Save Changes")>Save</button>
No selector. No binding mess. Just functionality where you need it.
đ§âđť DX Boost: Why Developers Will Love It
- â Type-safe, IDE-friendly syntax
- â Less boilerplate for tiny behaviors
- â Cleaner APIs for design systems
- â Reusability without creating custom components
- â Perfect for modern, composable architecture
Selectorless directives basically give Angular the hooks it never had.
đ ď¸ When (and When Not) to Use Selectorless Directives
â Use When:
- You need to apply lightweight behavior across multiple elements
- Youâre enhancing built-in elements (like
<button>
or<input>
) - You want to keep markup clean and behavior modular
- You’re building reusable utilities or shared modules
â Avoid When:
- You need to make major DOM changes or add complex templates
- Your logic depends heavily on a specific component tree
- Youâre already using deeply integrated components
đ Final Thoughts
Selectorless directives are one of the most elegant additions to Angular in recent years. They encourage a functional, declarative, and scalable way of building UIsâwithout abandoning the structure and power that Angular is known for.
If you’re working on a large app, a design system, or just looking to clean up messy templates, this feature is absolutely worth trying.
đŹ Have you experimented with selectorless directives yet?
Drop your thoughts belowâIâd love to hear how you’re using them or what patterns you’re discovering.
Letâs make Angular cleaner and more composableâone directive at a time. đ