services, factories and providers in modern Angular
service, factory and provider all become one thing: a class marked with @Injectable.
Before · AngularJS
app.factory('UserService', function ($http) {
return {
getAll: function () { return $http.get('/api/users'); }
};
});After · Angular@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
getAll() {
return this.http.get<User[]>('/api/users');
}
}What changes
- providedIn: 'root' registers the service once for the whole app, like AngularJS singletons.
- Dependencies are requested with inject() and checked by TypeScript.
- constant and value become injection tokens or plain exported constants.
Migration tip. Services are a good place to start. Rewrite one in Angular, downgrade it, and legacy screens keep using it without knowing it changed.
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