Bird
0
0

You want to create a standalone component that uses Angular's built-in NgIf directive. How should you declare the component to use NgIf correctly?

hard📝 component behavior Q8 of 15
Angular - Standalone Components
You want to create a standalone component that uses Angular's built-in NgIf directive. How should you declare the component to use NgIf correctly?
A@Component({ standalone: true, template: `
Visible
` }) export class MyComponent { show = true; }
B@Component({ standalone: true, imports: [], template: `
Visible
` }) export class MyComponent { show = true; }
C@Component({ standalone: true, imports: [NgIf], template: `
Visible
` }) export class MyComponent { show = true; }
D@Component({ standalone: true, imports: [CommonModule], template: `
Visible
` }) export class MyComponent { show = true; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify module providing NgIf

    NgIf directive is part of CommonModule, so CommonModule must be imported.
  2. Step 2: Check imports array

    The correct declaration includes CommonModule in the imports array. Omitting imports or importing NgIf directly will cause compilation errors.
  3. Final Answer:

    Import CommonModule in imports array to use NgIf -> Option D
  4. Quick Check:

    NgIf requires CommonModule import = @Component({ standalone: true, imports: [CommonModule], template: `
    Visible
    ` }) export class MyComponent { show = true; } [OK]
Quick Trick: Import CommonModule to use NgIf in standalone components [OK]
Common Mistakes:
  • Not importing CommonModule when using NgIf
  • Trying to import NgIf directly
  • Leaving imports array empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes