DEV Community

Cover image for The Directive Composition API in Angular
Ilyoskhuja
Ilyoskhuja

Posted on

2

The Directive Composition API in Angular

The Directive Composition API in Angular provides a way to create custom directives that can be used to manipulate the behavior and appearance of elements in your templates. It allows you to define custom logic and use it to interact with elements in your views.

Here's a step-by-step guide on how to use the Directive Composition API in Angular:

Import the necessary classes:
In order to use the Directive Composition API, you'll first need to import the Directive and ViewContainerRef classes from the @angular/core module. These classes will be used to define your custom directive and manipulate elements in the view.

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

Enter fullscreen mode Exit fullscreen mode

Create a directive class:
Next, create a class for your custom directive and use the @Directive decorator to provide metadata about the directive. The selector property in the metadata should be a CSS selector that you'll use to apply the directive to elements in your templates.

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {

  constructor(private viewContainer: ViewContainerRef) { }

}

Enter fullscreen mode Exit fullscreen mode

Implement the directive logic:
In the directive class, you can implement the logic for the directive using the ViewContainerRef to manipulate elements in the view. For example, you can create an embedded view and use it to wrap the content of an element in your template with a yellow background.

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {

  constructor(private viewContainer: ViewContainerRef) { 
    this.viewContainer.createEmbeddedView(this.template);
  }

  private get template() {
    return `
      <ng-template>
        <div style="background-color: yellow;">
          <ng-content></ng-content>
        </div>
      </ng-template>
    `;
  }

}

Enter fullscreen mode Exit fullscreen mode

Register the directive in your module:
Finally, you'll need to register the directive in your module's declarations array. This will make the directive available for use in your templates.

@NgModule({
  declarations: [
    HighlightDirective
  ],
  ...
})
export class AppModule { }

Enter fullscreen mode Exit fullscreen mode

Use the directive in your templates:
You can now use the directive in your templates by applying the selector (in this case appHighlight) to any element. When the directive is applied, the content of the element will be wrapped in a yellow background.

<p appHighlight>This text will be highlighted</p>

Enter fullscreen mode Exit fullscreen mode

This is just a basic example of how to use the Directive Composition API in Angular. You can use it to create more complex custom directives as needed to fit your specific use case. The ability to manipulate elements in the view and interact with templates gives you a lot of flexibility and power when building Angular applications.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
naucode profile image
Al - Naucode •

Hey, that was a nice read, you got my follow, keep writing 😉

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more