Sub topic 3
Using the directive in a standalone component
This chapter assumes you know TypeScript classes and decorators, Angular components and templates, and the basics of standalone components. It also assumes you know property and event binding. You already have a HighlightDirective with the selector [appHighlight]. Declaring it does nothing on its own. A template has to opt in to it, and this chapter shows how.
The directive from the previous step
Here is the directive again, so the chapter stands alone:
import { Directive, ElementRef, inject } from '@angular/core';
@Directive({
selector: '[appHighlight]',
})
export class HighlightDirective {
private el = inject<ElementRef<HTMLElement>>(ElementRef);
constructor() {
this.el.nativeElement.style.backgroundColor = 'yellow';
}
}In Angular 19 and later, directives are standalone by default. In versions 14 to 18, add standalone: true to the decorator.
Importing the directive
A standalone component compiles its template against its own imports array. Nothing is shared through a global module. To use a directive, list its class in imports, then add its selector to an element:
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'app-demo',
imports: [HighlightDirective],
template: `<p appHighlight>This paragraph is highlighted.</p>`,
})
export class DemoComponent {}The selector [appHighlight] is a CSS attribute selector. Angular creates one HighlightDirective instance for every element in this template that has the attribute. The element carrying the directive is called the host element.
Matching happens when the template is compiled. It is a lookup against imports, not against every directive in the project.
Applying it to many elements and to components
You can place the attribute on any number of elements. Each one gets its own instance with its own state:
<p appHighlight>First</p>
<p appHighlight>Second</p>
<app-card appHighlight>A component can be a host too</app-card>When the host is a component, the directive works on that component's outer element, the <app-card> tag. It does not reach inside the component's template.
Some directives are used by several components. Import the directive in each component that uses it. If a template uses many shared directives, you can group them in a constant array and spread it into imports:
export const SHARED_DIRECTIVES = [HighlightDirective] as const;Then write imports: [...SHARED_DIRECTIVES].
An NgModule-based component can use a standalone directive too. Put the directive in the module's imports array, not in declarations.
Common errors when applying a directive
The most frequent mistake is forgetting the import. What happens next depends on how you wrote the attribute.
| Template | Directive not imported | Directive imported |
|---|---|---|
| `<p appHighlight>` | No error. The attribute is plain HTML and nothing happens. | Directive runs. |
| `<p [appHighlight]="'red'">` | Compile error NG8002: can't bind to `appHighlight` because it isn't a known property of `p`. | Works if the directive has a matching input. |
The first row is the dangerous one. A static attribute fails silently, so a directive that "does nothing" usually means a missing import or a typo in the selector. Check these in order:
- Is the class in the
importsarray of the component that owns the template? - Does the attribute spelling match the selector exactly, including case?
apphighlightwill not match[appHighlight]. - Is the element inside the template of that component, and not in a child component's template?
The third point matters. Imports do not pass down to child components. A child that uses appHighlight needs its own import.
Why the prefix matters
The app prefix in appHighlight keeps your selector from clashing with a native HTML attribute or a library directive. If two imported directives match the same element, Angular creates both. It does not pick one. Use a project prefix on every custom selector.
Check the result
Run the app and open the page. Every element with appHighlight should have a yellow background. Delete the imports entry and reload. The background disappears and no error appears. That is the silent failure from the table above, so learn to recognize it.
Exercise
A template contains <li [appHighlight]="'pink'">Item</li> and the build fails with NG8002 saying appHighlight is not a known property of li. The directive class exists and its selector is [appHighlight]. What is the most likely cause, and what is the fix?
Show answer
The component that owns the template does not list HighlightDirective in its imports array, so the compiler never matched the selector. Add HighlightDirective to imports. If the error remains, check that the directive defines an input named appHighlight, because a property binding needs an input to bind to.
Exercise
You add <app-card appHighlight> inside ParentComponent. ParentComponent imports HighlightDirective, and CardComponent does not. Does the highlight apply, and does CardComponent need the import?
Show answer
The highlight applies to the <app-card> host element, because the attribute is written in ParentComponent's template, which imports the directive. CardComponent does not need the import unless its own template also uses appHighlight.