Sub topic 8
Reading a Directive from the Template with exportAs
This chapter assumes you can write a standalone attribute directive, apply it through a component's imports array, and use signals. It also assumes you know template reference variables such as #box. By default a directive's members are invisible to the template. The exportAs option changes that.
The problem exportAs solves
A directive normally works in the background. The template applies it, and the directive changes its host element. Sometimes another element in the template needs to talk to it, for example a button that toggles the highlight.
Without exportAs, a template reference variable on the host points to the DOM element, not to the directive:
<p appHighlight #hl>Text</p>
<!-- hl is an HTMLParagraphElement, not a HighlightDirective -->The element has no toggle() method, so the template can't call one.
Exporting the directive instance
Add exportAs to the decorator. Its value is a name that template reference variables can use to ask for the directive instance.
import { Directive, signal } from '@angular/core';
@Directive({
selector: '[appHighlight]',
exportAs: 'appHighlight',
host: {
'[style.backgroundColor]': 'active() ? "yellow" : null',
},
})
export class HighlightDirective {
readonly active = signal(false);
toggle() {
this.active.update((value) => !value);
}
}The host binding turns the background yellow while active() is true. The toggle() method and the active signal are public, so a template can use them.
Binding the instance to a template variable
Write the export name as the value of the reference variable: #hl="appHighlight".
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'app-demo',
imports: [HighlightDirective],
template: `
<p appHighlight #hl="appHighlight">Toggle my background.</p>
<button type="button" (click)="hl.toggle()">Toggle</button>
<span>Highlight is {{ hl.active() ? 'on' : 'off' }}</span>
`,
})
export class DemoComponent {}Here hl is the HighlightDirective instance. The button calls its method, and the span reads its signal. Because active is a signal, the text updates when it changes.
Rules to remember
- The export name does not have to match the selector.
exportAs: 'highlight'is valid, and you would write#hl="highlight". Matching the selector is a common convention, but a short name can read better. - You can export several names with a comma-separated string, such as
exportAs: 'appHighlight, highlight'. This is rarely needed. - The variable is visible only in the template where you declare it, including child elements and embedded views such as
@ifblocks. Other components and the class body can't see it. - Only public members are usable. Keep the exported surface small, because anything public becomes part of your directive's API.
Reading the instance from the class
If the component class needs the same instance, query for it by the variable name. A query on a reference variable with an export name returns the directive instance, not the element.
import { Component, viewChild } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'app-panel',
imports: [HighlightDirective],
template: `<p appHighlight #hl="appHighlight">Panel text</p>`,
})
export class PanelComponent {
private readonly highlight = viewChild.required<HighlightDirective>('hl');
flash() {
this.highlight().toggle();
}
}Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `NG0301: Export of name 'appHighlight' not found!` | The directive is not in the component's `imports`, or `exportAs` is missing or spelled differently. | Import the directive and make the names match exactly. |
| `hl.toggle is not a function` or a type error | You wrote `#hl` with no value, so `hl` is the DOM element. | Write `#hl="appHighlight"`. |
| The variable is undefined outside the template | Reference variables are scoped to the template. | Use `viewChild` in the class. |
| The wrong directive is exported | Two directives on one element use the same export name. | Give each directive a unique `exportAs`. |
Exercise
A directive has the selector [appTooltip] and exportAs: 'tooltip'. It has a public show() method. Write the template for a <span> that uses the directive and a button that calls show().
Show answer
Use <span appTooltip #tip="tooltip">Hint</span> and <button type="button" (click)="tip.show()">Show</button>. The value of the reference variable must be the export name tooltip, not the selector appTooltip. The component must list the directive in its imports array, or Angular reports NG0301.