0
0
Angularframework~20 mins

Standalone pipes and directives in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Standalone Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a standalone directive affect component usage?

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?

Angular
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>
AAngular throws a runtime error about missing directive import.
BThe text will be red because standalone directives are globally available.
CThe text will not be red because the directive is not imported in the component or module.
DThe text color changes only if the directive is declared in a module.
Attempts:
2 left
💡 Hint

Standalone directives must be imported where used unless explicitly provided globally.

📝 Syntax
intermediate
2:00remaining
Correct syntax to declare a standalone pipe

Which of the following is the correct way to declare a standalone Angular pipe named ExclaimPipe that adds an exclamation mark?

A
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'exclaim', standalone: false})
export class ExclaimPipe implements PipeTransform {
  transform(value: string): string {
    return value + '!';
  }
}
B
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({standalone: true})
export class ExclaimPipe implements PipeTransform {
  transform(value: string): string {
    return value + '!';
  }
}
C
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'exclaim'})
export class ExclaimPipe implements PipeTransform {
  transform(value: string): string {
    return value + '!';
  }
}
D
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'exclaim', standalone: true})
export class ExclaimPipe implements PipeTransform {
  transform(value: string): string {
    return value + '!';
  }
}
Attempts:
2 left
💡 Hint

Standalone pipes require the standalone: true flag and a name.

🔧 Debug
advanced
2:00remaining
Why does this standalone directive not apply styles?

Given this standalone directive, why does the style not apply when used in a component?

Angular
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 {}
AThe directive is not imported in the component's imports array, so Angular ignores it.
BThe directive selector is incorrect; it should be 'appBold' without brackets.
CRenderer2 cannot set styles in standalone directives.
DThe component must declare the directive in declarations, not imports.
Attempts:
2 left
💡 Hint

Standalone components must import standalone directives explicitly.

state_output
advanced
2:00remaining
What is the output of this standalone pipe usage?

What will be the rendered output of this Angular component using a standalone pipe ReversePipe?

Angular
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';
}
A<p>olleh</p>
B<p>hello</p>
C<p>error: pipe not found</p>
D<p>undefined</p>
Attempts:
2 left
💡 Hint

The pipe reverses the string and is imported correctly.

🧠 Conceptual
expert
2:00remaining
Why use standalone directives and pipes in Angular?

Which of the following is the main advantage of using standalone directives and pipes in Angular?

AThey allow components to be used without importing any dependencies.
BThey simplify dependency management by allowing direct imports without NgModules.
CThey automatically make directives and pipes global across the app.
DThey replace the need for Angular modules entirely.
Attempts:
2 left
💡 Hint

Think about how standalone features affect imports and modules.