Consider an Angular standalone directive HighlightDirective that changes text color. If you apply it to a component's template without importing it in the component, what happens?
import { Directive, ElementRef, Renderer2 } from '@angular/core'; @Directive({ selector: '[appHighlight]', standalone: true }) export class HighlightDirective { constructor(el: ElementRef, renderer: Renderer2) { renderer.setStyle(el.nativeElement, 'color', 'red'); } } // Component template: // <p appHighlight>This text should be red.</p>
Standalone directives must be imported where used unless explicitly provided globally.
Standalone directives are not automatically global. You must import them in the component's imports array or in a module to use them.
Which of the following is the correct way to declare a standalone Angular pipe named ExclaimPipe that adds an exclamation mark?
Standalone pipes require the standalone: true flag and a name.
Option D correctly declares a standalone pipe with a name. Option D misses the name, C is not standalone, and A explicitly disables standalone.
Given this standalone directive, why does the style not apply when used in a component?
import { Directive, ElementRef, Renderer2 } from '@angular/core'; @Directive({ selector: '[appBold]', standalone: true }) export class BoldDirective { constructor(el: ElementRef, renderer: Renderer2) { renderer.setStyle(el.nativeElement, 'font-weight', 'bold'); } } // Component code: // @Component({ // selector: 'app-sample', // template: '<p appBold>Bold text</p>', // standalone: true, // imports: [] // }) // export class SampleComponent {}
Standalone components must import standalone directives explicitly.
Standalone directives must be imported in the component's imports array to be recognized. Without import, Angular ignores the directive.
What will be the rendered output of this Angular component using a standalone pipe ReversePipe?
import { Component } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'reverse', standalone: true}) export class ReversePipe implements PipeTransform { transform(value: string): string { return value.split('').reverse().join(''); } } @Component({ selector: 'app-root', template: '<p>{{ message | reverse }}</p>', standalone: true, imports: [ReversePipe] }) export class AppComponent { message = 'hello'; }
The pipe reverses the string and is imported correctly.
The ReversePipe reverses the string 'hello' to 'olleh'. It is standalone and imported, so it works as expected.
Which of the following is the main advantage of using standalone directives and pipes in Angular?
Think about how standalone features affect imports and modules.
Standalone directives and pipes let you import them directly in components, reducing the need to manage NgModules. They do not make features global automatically nor replace modules entirely.