Assuming this.http is properly injected and data is a class property.
medium
A. GET request URL is invalid
B. HttpClient is not imported
C. Missing error handling in subscribe
D. subscribe callback uses wrong arrow function syntax
Solution
Step 1: Check syntax and usage
The get() call and subscribe syntax are correct, and this.http is assumed injected.
Step 2: Identify missing best practice
No error handling is provided in subscribe, which can cause silent failures.
Final Answer:
Missing error handling in subscribe -> Option C
Quick Check:
Always add error handling in subscribe [OK]
Hint: Always handle errors in subscribe to catch failures [OK]
Common Mistakes:
Ignoring error callback in subscribe
Assuming URL is invalid without checking
Confusing arrow function syntax
5. You want to load a list of users from an API and display them in your Angular component. Which approach correctly combines GET request and updating the component's state?
hard
A. Call this.http.post('api/users').subscribe(users => this.users = users); in constructor
B. Use this.http.get('api/users').subscribe(users => this.users = users); in ngOnInit
C. Assign this.users = this.http.get('api/users'); directly without subscribe
D. Use this.http.delete('api/users').subscribe(users => this.users = users); in ngOnInit
Solution
Step 1: Choose correct HTTP method and lifecycle hook
GET is used to fetch data; ngOnInit is the right place to load data after component creation.
Step 2: Use subscribe to update component state
Subscribe receives data asynchronously and assigns it to this.users for display.
Final Answer:
Use this.http.get('api/users').subscribe(users => this.users = users); in ngOnInit -> Option B
Quick Check:
GET + subscribe + ngOnInit = correct pattern [OK]
Hint: Load data in ngOnInit with http.get() and subscribe [OK]
Common Mistakes:
Using POST or DELETE instead of GET
Assigning observable directly without subscribe
Loading data in constructor instead of ngOnInit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.