$http in modern Angular
$http becomes HttpClient. It returns typed observables and gives you the response body directly instead of res.data.
Before · AngularJS
$http.get('/api/users', { params: { page: 1 } })
.then(function (res) { vm.users = res.data; })
.catch(function (err) { vm.error = err.statusText; });After · Angularprivate http = inject(HttpClient);
getUsers(page: number) {
return this.http.get<User[]>('/api/users', { params: { page } });
}
// app.config.ts
providers: [provideHttpClient(withInterceptors([authInterceptor]))]What changes
- Responses are typed: get<User[]>() instead of an untyped res.data.
- Requests are lazy: nothing is sent until something subscribes.
- Requests can be cancelled, retried and combined with RxJS operators.
Migration tip. Migrate the HTTP layer first and expose it to the AngularJS side. Old and new screens then share one data layer during the whole migration.
Related
Planning a migration or a micro-frontend platform?
In a 20-minute call you describe your situation and I tell you how I'd approach it. If it isn't worth doing yet, I'll say so.
Message me on LinkedIn