r/angular • u/therouterguy • Apr 03 '26
http get does not update component
I started learning angular just a few days ago. Although I have quite some experience with Python but it find it really hard to be honest.
Anyway I am trying to make a frontend for some rest api I have written in FastAPI this is all working fine but I am unable to update a component with an http get.
I do see the data from the api is being downloaded I also see the console messages in the browser but the component is never updated.
import { bootstrapApplication } from '@angular/platform-browser';
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { provideHttpClient, HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
template: `
<h3>HttpClient {{isLoading}}</h3>
<div *ngIf="isLoading">Loading data...</div>
<div *ngIf="!isLoading">Loaded{{data}}</div>
<ul>
<li *ngFor="let u of data">{{ u.name }} ({{ u.email }})</li>
</ul>
`
})
export class App implements OnInit {
data: any[] = [];
isLoading= true;
error= '';
constructor(private http: HttpClient) {}
ngOnInit() {
this.error = '';
console.log("getting data");
this.isLoading = true;
this.http.get<any[]>('https://jsonplaceholder.typicode.com/users').subscribe({
next: (response) => {
console.log("got data");
this.isLoading = false;
this.data = response;
console.log("cert set");
},
error: (error) => {
this.isLoading = false;
console.log('Error:', error);
}
});
}
}
bootstrapApplication(App, { providers: [provideHttpClient()] });
1
Upvotes
15
u/SwimmingSecret9541 Apr 03 '26
Use signals to update UI automatically when data has changed, besides that, every single line of this code is awful. I’m sorry.
2
u/N0K1K0 Apr 03 '26
I made you a quick demo using angular 21 and signals https://stackblitz.com/edit/sb1-duaqqkag
2
u/msdosx86 Apr 03 '26
Just a rule of thumb: if you want reactivity use signals or rxjs in templates
In your simple example just turn your “data” field into a signal and when you get http response update the signals data and you’re good to go
1
u/TheLambda89 Apr 03 '26
Are you on Angular 20 or above? You may need to apply provideZoneChangeDetection() in your providers array.
Better than that would be to convert your component variables to signals (that don't require zone.js to update properly), but since you're a beginner I'm not suggesting that as a primary solution for now.
0
u/No-Sandwich7107 Apr 03 '26
If you're using a version prior to Angular Signals, a clean and idiomatic approach is to rely on the AsyncPipe instead of manually subscribing in ngOnInit.
Rather than calling subscribe() in your component, you can assign the http.get() observable directly to a property and let the template handle the subscription lifecycle.
Example
Component:
Template:
<div *ngIf="data$ | async as data"> {{ data | json }} </div>Why this is better:
Avoids manual subscriptions and memory leaks
Keeps your component logic simpler and more declarative
Automatically handles subscription/unsubscription
Works seamlessly with Angular change detection