An attribute directive adds behavior to an existing element. Declare it with a selector in brackets, make it standalone, and put reusable DOM behavior in the directive class. ```ts import { Directive, ElementRef, Renderer2, input } from '@angular/core'; @Directive({ selector: '[appHighlight]', standalone: true, host: { '(mouseenter)': 'highlight()', '(mouseleave)': 'clearHighlight()', }, }) export class HighlightDirective { readonly appHighlight = input('yellow'); constructor( private readonl...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-directives-pipes --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Angular Directives Pipes?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-directives-pipes-agent-skills-standard)More formats (shields.io, HTML) on the badges page.
# Creating a custom attribute directive
An attribute directive adds behavior to an existing element. Declare it with a selector in brackets, make it standalone, and put reusable DOM behavior in the directive class.
```ts
import { Directive, ElementRef, Renderer2, input } from '@angular/core';
@Directive({
selector: '[appHighlight]',
standalone: true,
host: {
'(mouseenter)': 'highlight()',
'(mouseleave)': 'clearHighlight()',
},
})
export class HighlightDirective {
readonly appHighlight = input('yellow');
constructor(
private readonly element: ElementRef<HTMLElement>,
private readonly renderer: Renderer2,
) {}
highlight(): void {
this.renderer.setStyle(
this.element.nativeElement,
'background-color',
this.appHighlight(),
);
}
clearHighlight(): void {
this.renderer.removeStyle(this.element.nativeElement, 'background-color');
}
}
```
Import the directive directly into a standalone component and use its selector as an attribute:
```ts
@Component({
standalone: true,
imports: [HighlightDirective],
template: `
<p appHighlight>Hover for the default highlight.</p>
<p [appHighlight]="'lightblue'">Hover for a custom color.</p>
`,
})
export class ExampleComponent {}
```
The `host` object wires host events to directive methods. This keeps the directive standalone and avoids deprecated-style `@HostBinding` and `@HostListener` decorators. Use `Renderer2` for DOM writes rather than manipulating the element directly.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!