Bird
0
0

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🚀 Application Q15 of 15
Angular - HTTP Client
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?
ACall <code>this.http.post('api/users').subscribe(users => this.users = users);</code> in constructor
BUse <code>this.http.get('api/users').subscribe(users => this.users = users);</code> in ngOnInit
CAssign <code>this.users = this.http.get('api/users');</code> directly without subscribe
DUse <code>this.http.delete('api/users').subscribe(users => this.users = users);</code> in ngOnInit
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Use subscribe to update component state

    Subscribe receives data asynchronously and assigns it to this.users for display.
  3. Final Answer:

    Use this.http.get('api/users').subscribe(users => this.users = users); in ngOnInit -> Option B
  4. Quick Check:

    GET + subscribe + ngOnInit = correct pattern [OK]
Quick Trick: Load data in ngOnInit with http.get() and subscribe [OK]
Common Mistakes:
MISTAKES
  • Using POST or DELETE instead of GET
  • Assigning observable directly without subscribe
  • Loading data in constructor instead of ngOnInit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes