Consider this Angular standalone component that fetches user data on initialization.
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-user',
standalone: true,
template: `<div>User: {{ user?.name }}</div>`
})
export class UserComponent implements OnInit {
user: any;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('https://api.example.com/user/1').subscribe(data => {
this.user = data;
});
}
}Assuming the GET request returns {"name": "Alice"}, what will be shown in the browser?
import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-user', standalone: true, template: `<div>User: {{ user?.name }}</div>` }) export class UserComponent implements OnInit { user: any; constructor(private http: HttpClient) {} ngOnInit() { this.http.get('https://api.example.com/user/1').subscribe(data => { this.user = data; }); } }
Look at how the template uses the safe navigation operator ?. and what the GET request returns.
The GET request returns an object with a name property set to "Alice". The template safely accesses user?.name, so it displays "User: Alice" once the data arrives.
Choose the code snippet that correctly uses Angular's HttpClient to perform a GET request and subscribe to the response.
Remember that Angular's HttpClient returns an Observable, not a Promise.
Angular's HttpClient returns an Observable, so you use subscribe() to get the response. Options A, C, and D use invalid methods.
Look at this Angular service method:
getData() {
return this.http.get('https://external-api.com/data');
}When called, the browser console shows a CORS error. What is the most likely cause?
getData() {
return this.http.get('https://external-api.com/data');
}CORS errors happen when the server blocks requests from different origins.
CORS (Cross-Origin Resource Sharing) errors occur because the external API server does not permit requests from your app's domain. This is a server-side configuration issue.
Given this Angular component code:
data: any;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('https://api.example.com/items').subscribe(response => {
this.data = response;
});
}If the API returns [{"id":1,"name":"Item1"},{"id":2,"name":"Item2"}], what will data contain?
data: any;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('https://api.example.com/items').subscribe(response => {
this.data = response;
});
}The API returns an array of objects. The subscription assigns the whole response to data.
The GET request returns an array of two objects. The data variable holds the entire array after subscription.
Choose the statement that correctly describes how Angular's HttpClient GET requests work.
Think about how Angular handles HTTP requests and Observables.
Angular's HttpClient GET returns an Observable that emits the HTTP response once and then completes. It does not emit repeatedly or return a Promise.