Bird
0
0

How can you combine *ngIf with a custom pipe to conditionally render content only if a transformed value is truthy? For example, show a message only if user.name | trim is not empty.

hard📝 Application Q9 of 15
Angular - Directives
How can you combine *ngIf with a custom pipe to conditionally render content only if a transformed value is truthy? For example, show a message only if user.name | trim is not empty.
A
Hello {{ user.name }}
B
Hello {{ user.name }}
C
Hello {{ user.name }}
D
Hello {{ user.name }}
Step-by-Step Solution
Solution:
  1. Step 1: Understand pipe usage in *ngIf

    Pipes must be wrapped in parentheses when used inside *ngIf to ensure correct evaluation.
  2. Step 2: Evaluate options

    Hello {{ user.name }}
    correctly wraps the pipe expression in parentheses.
    Hello {{ user.name }}
    lacks parentheses, causing syntax error.
    Hello {{ user.name }}
    compares pipe output incorrectly inside *ngIf.
    Hello {{ user.name }}
    uses method call which is invalid in template expressions.
  3. Final Answer:

    <div *ngIf="(user.name | trim)">Hello {{ user.name }}</div> -> Option C
  4. Quick Check:

    Wrap pipe in parentheses inside *ngIf [OK]
Quick Trick: Use parentheses around pipe expressions in *ngIf [OK]
Common Mistakes:
  • Omitting parentheses around pipe
  • Using method calls in template expressions
  • Incorrect comparison inside *ngIf

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes