Standalone pipes and directives let you use them without adding to a module. This makes your code simpler and easier to manage.
Standalone pipes and directives in Angular
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'examplePipe', standalone: true }) export class ExamplePipe implements PipeTransform { transform(value: string): string { return value.toUpperCase(); } } import { Directive } from '@angular/core'; @Directive({ selector: '[exampleDirective]', standalone: true }) export class ExampleDirective { // directive logic here }
Use standalone: true in the decorator to make a pipe or directive standalone.
Standalone pipes and directives can be imported directly into components.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'reverseText', standalone: true }) export class ReverseTextPipe implements PipeTransform { transform(value: string): string { return value.split('').reverse().join(''); } }
import { Directive, ElementRef, Renderer2 } from '@angular/core'; @Directive({ selector: '[highlight]', standalone: true }) export class HighlightDirective { constructor(private el: ElementRef, private renderer: Renderer2) { this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'yellow'); } }
This standalone component uses the standalone pipe and directive directly without a module. The text 'hello' is reversed and the heading is highlighted.
import { Component } from '@angular/core'; import { ReverseTextPipe } from './reverse-text.pipe'; import { HighlightDirective } from './highlight.directive'; @Component({ selector: 'app-root', standalone: true, imports: [ReverseTextPipe, HighlightDirective], template: ` <h1 highlight>{{ 'hello' | reverseText }}</h1> ` }) export class AppComponent {}
Standalone pipes and directives simplify Angular app structure by removing the need for NgModules.
Remember to add standalone pipes and directives to the imports array of the component using them.
This feature requires Angular 14 or later.
Standalone pipes and directives work without modules.
Use standalone: true in their decorators.
Import them directly into components for easy reuse.
Practice
standalone: true in an Angular pipe or directive decorator do?Solution
Step 1: Understand the role of standalone flag
Thestandalone: trueflag in Angular marks a pipe or directive so it does not require declaration inside an NgModule.Step 2: Effect on usage
This means you can import the standalone pipe or directive directly into components without needing a module.Final Answer:
It allows the pipe or directive to be used without declaring it in an NgModule. -> Option BQuick Check:
standalone: true means no NgModule needed [OK]
- Thinking standalone makes directive private
- Assuming standalone disables usage
- Believing standalone auto-imports everywhere
Solution
Step 1: Check selector syntax
For attribute directives, the selector must be in square brackets, e.g.,'[appHighlight]'.Step 2: Verify standalone flag
To make a directive standalone,standalone: truemust be set in the decorator.Final Answer:
@Directive({ selector: '[appHighlight]', standalone: true }) -> Option DQuick Check:
Standalone directive needs selector with [] and standalone: true [OK]
- Missing square brackets in selector for attribute directive
- Setting standalone to false or omitting it
- Using element selector instead of attribute selector
@Pipe({name: 'exclaim', standalone: true})
export class ExclaimPipe implements PipeTransform {
transform(value: string): string {
return value + '!';
}
}What will be the output of this template?
<div>{{ 'Hello' | exclaim }}</div>Solution
Step 1: Check pipe declaration and usage
The pipe is standalone and must be imported into the component using it.Step 2: Analyze template usage without import
If the component does not import the standalone pipe, Angular will not recognize it, causing an error.Final Answer:
Error: Pipe 'exclaim' not found -> Option AQuick Check:
Standalone pipe must be imported to use [OK]
- Assuming standalone pipes auto-import
- Expecting output without importing pipe
- Confusing pipe transform logic with usage
@Directive({ selector: '[appColor]', standalone: true })
export class ColorDirective {
constructor(private el: ElementRef) {
el.nativeElement.style.color = 'red';
}
}When you use
<div appColor>Text</div> in a component template but forget to import ColorDirective in the component, what happens?Solution
Step 1: Understand standalone directive import
Standalone directives must be imported into the component's imports array to be recognized.Step 2: Effect of missing import
If the directive is not imported, Angular does not know about it and throws a template parse error.Final Answer:
Angular throws a template parse error about unknown directive. -> Option CQuick Check:
Missing import causes template parse error [OK]
- Assuming directive works without import
- Expecting default styles without directive
- Confusing runtime errors with template errors
1. Add
standalone: true in the pipe decorator.2. Import the pipe in each component's
imports array.3. Declare the pipe in a shared NgModule.
4. Use the pipe in templates after importing.
Choose the correct combination.
Solution
Step 1: Understand standalone pipe creation
Addingstandalone: trueallows the pipe to be used without NgModule declaration.Step 2: Import in components and use
Each component that uses the pipe must import it in itsimportsarray and then use it in templates.Step 3: NgModule declaration is unnecessary
Declaring the pipe in a shared NgModule is not needed and contradicts standalone usage.Final Answer:
Steps 1, 2, and 4 only -> Option AQuick Check:
Standalone pipe needs standalone: true, import in components, use in template [OK]
- Declaring standalone pipes in NgModules
- Forgetting to import pipe in components
- Assuming usage without import
Ready to prove it?
Pass the test and this pattern moves unknown
