Bird
0
0

You want to send a GET request with multiple query parameters, some optional. How do you build HttpParams to include only parameters with non-empty values?

hard🚀 Application Q8 of 15
Angular - HTTP Client
You want to send a GET request with multiple query parameters, some optional. How do you build HttpParams to include only parameters with non-empty values?
ACall set() for all parameters regardless of value
BCreate HttpParams with all parameters at once, including empty
CUse append() for all parameters including empty
DUse a loop to call set() only if value is not empty
Step-by-Step Solution
Solution:
  1. Step 1: Filter parameters before adding

    To avoid empty query parameters, check each value before adding it to HttpParams.
  2. Step 2: Use set() conditionally

    Call set() only for parameters with non-empty values, building HttpParams immutably.
  3. Final Answer:

    Use a loop to call set() only if value is not empty -> Option D
  4. Quick Check:

    Filter params before set() [OK]
Quick Trick: Add params conditionally to avoid empty query strings [OK]
Common Mistakes:
MISTAKES
  • Adding empty parameters causing unwanted URL parts
  • Using append() which adds duplicates
  • Not reassigning HttpParams after set()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes