Carlos Cifuentes
AngularJS → Angular reference

constant / value in modern Angular

A constant that never changes becomes a plain exported const. A value that differs per environment or in tests becomes an InjectionToken.

Before · AngularJS
app.constant('API_URL', '/api/v2');
app.constant('PAGE_SIZES', [10, 25, 50]);
app.value('appConfig', { theme: 'light', locale: 'en' });

app.service('UserService', function ($http, API_URL) {
  this.list = function () { return $http.get(API_URL + '/users'); };
});
After · Angular
// plain constant: no DI needed
export const PAGE_SIZES = [10, 25, 50] as const;

// value that changes per environment or in tests
export const API_URL = new InjectionToken<string>('API_URL', {
  providedIn: 'root',
  factory: () => '/api/v2',
});

@Injectable({ providedIn: 'root' })
export class UserService {
  private http = inject(HttpClient);
  private apiUrl = inject(API_URL);
  list() { return this.http.get<User[]>(`${this.apiUrl}/users`); }
}

What changes

  • app.constant values are imported with ES modules; no injection is involved.
  • InjectionToken<T> gives the value a type and a default factory, and can be overridden with { provide: API_URL, useValue: ... }.
  • Mutable app.value objects that several parts of the app modify become a service with signals.

Migration tip. Most AngularJS constants lived in DI because there were no ES modules to import them from. Export them as plain constants and keep tokens for the few values you really override.

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