Complete the code to declare a standalone pipe in Angular.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'exclaim', standalone: [1] }) export class ExclaimPipe implements PipeTransform { transform(value: string): string { return value + '!'; } }
Setting standalone: true makes the pipe standalone, so it can be used without declaring it in an NgModule.
Complete the code to declare a standalone directive in Angular.
import { Directive, ElementRef, Renderer2 } from '@angular/core'; @Directive({ selector: '[highlight]', standalone: [1] }) export class HighlightDirective { constructor(private el: ElementRef, private renderer: Renderer2) { this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'yellow'); } }
Standalone directives must have standalone: true to be used without NgModules.
Fix the error in the standalone pipe declaration by completing the missing property.
import { Pipe, PipeTransform } from '@angular/core'; import { CommonModule } from '@angular/common'; @Pipe({ name: 'reverse', standalone: true, [1]: [CommonModule] }) export class ReversePipe implements PipeTransform { transform(value: string): string { return value.split('').reverse().join(''); } }
The imports property is used to import other standalone modules or Angular modules needed by the pipe.
Fill both blanks to create a standalone directive that imports CommonModule and exports itself.
import { Directive } from '@angular/core'; import { CommonModule } from '@angular/common'; @Directive({ selector: '[blink]', standalone: true, [1]: [CommonModule], [2]: [BlinkDirective] }) export class BlinkDirective {}
The imports property allows the directive to use CommonModule features. The exports property makes the directive available to other components or modules.
Fill all three blanks to create a standalone pipe that imports CommonModule, exports itself, and has a pure transform method.
import { Pipe, PipeTransform } from '@angular/core'; import { CommonModule } from '@angular/common'; @Pipe({ name: 'capitalize', standalone: true, [1]: [CommonModule], [2]: [CapitalizePipe], pure: [3] }) export class CapitalizePipe implements PipeTransform { transform(value: string): string { return value.charAt(0).toUpperCase() + value.slice(1); } }
The pipe imports CommonModule, exports itself for use elsewhere, and is marked pure for performance optimization.