Angular Directives Pipes
ASecurityAn 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...
- 584 stars
- 0 votes
- 0 copies
- 5 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 18 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-directives-pipes --agent claude-codeAre you the author of Angular Directives Pipes?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-directives-pipes-agent-skills-standard)# 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.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- eval-6.baseline.md
- eval-6.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.