module.config() / module.run() in modern Angular
config and run blocks become entries in the providers array of app.config.ts. Configuration is passed to provide functions, and startup code goes in provideAppInitializer.
Before · AngularJS
angular.module('app')
.config(function ($httpProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push('authInterceptor');
})
.run(function (SessionService) {
SessionService.restore();
});After · Angular// app.config.ts
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withInterceptors([authInterceptor])),
provideAppInitializer(() => inject(SessionService).restore()),
],
};
// main.ts
bootstrapApplication(AppComponent, appConfig);What changes
- Provider configuration ($httpProvider, $locationProvider) becomes options on provide functions, such as withInterceptors() or withHashLocation().
- A run block becomes provideAppInitializer (Angular 19 and later; before that, the APP_INITIALIZER token), which can use inject().
- If the initializer returns a promise or an observable, Angular waits for it before rendering. A run block could not wait.
Migration tip. Read every run block line by line before migrating. They collect unrelated things over the years (session restore, analytics, $rootScope listeners), and each one belongs in a different place: an initializer, a guard or a service.
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