Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Angular 22: why your screen stops updating after the upgrade, and how to fix it

You upgrade to Angular 22. The build passes and the tests are green. Then someone tells you the list doesn't refresh after saving. The console is quiet, the data is correct, and the screen is simply out of date. This pos

You upgrade to Angular 22. The build passes and the tests are green. Then someone tells you the list doesn't refresh after saving. The console is quiet, the data is correct, and the screen is simply out of date. This post explains why that happens in Angular 22 and how to find and fix it.

Two defaults changed, and they work together

In Angular 22, a component that doesn't set changeDetection now uses OnPush. The old behaviour has a name, ChangeDetectionStrategy.Eager, and you set it explicitly if you want to keep it (v22.0.0 release notes). On top of that, zoneless change detection has been the default for new applications since Angular 21.

With Zone.js, Angular re-checked your screens after almost every event, timer and request. Without it, Angular only checks when it is told something changed. According to the official zoneless guide, that happens when:

  • markForCheck() is called (the async pipe does this for you)
  • ComponentRef.setInput is used
  • a signal that the template reads is updated
  • a template or host listener runs

Changing a plain property on a class is not on that list. So the data changes, nobody tells Angular, and the screen stays as it was.

The classic bug: changing a plain field

This code worked for years under default change detection. It is the first thing to look for after the upgrade:

items: Item[] = [];

save(item: Item) {
  this.api.save(item).subscribe(saved => {
    this.items.push(saved); // the array changes, Angular is not told
  });
}

The fix is to keep the state in a signal, because a signal tells Angular exactly what changed:

items = signal<Item[]>([]);

save(item: Item) {
  this.api.save(item).subscribe(saved => {
    this.items.update(list => [...list, saved]);
  });
}

In the template, read it with items(), for example @for (item of items(); track item.id). I wrote more about how signals and OnPush fit together in Angular signals and OnPush: a practical guide.

Other places where it hides

  • Reactive forms. The guide says model updates such as setValue, patchValue and FormArray.push don't schedule change detection on their own. Connect the form's observables to markForCheck(), or reflect the data through signals.
  • Callbacks from other libraries. A chart, map or editor library that calls your code and changes a field is the same problem as the push above.
  • Code that waits for the zone. NgZone.onMicrotaskEmpty, onUnstable and onStable never emit in a zoneless app, and NgZone.isStable is always true. Use afterNextRender or afterEveryRender instead.
  • Scattered markForCheck() calls. They still work, but a screen full of them usually means the state should be in signals.

How to find these bugs before your users do

  1. Read the migration diff. ng update adds ChangeDetectionStrategy.Eager where applicable, so those components keep their old behaviour. The release notes also list a later fix for invalid code produced by that migration, so review what it wrote. Then move components to OnPush one at a time instead of all at once.
  2. Turn on the exhaustive check in development. provideCheckNoChangesConfig({ exhaustive: true, interval: 1000 }) checks from time to time for bindings that changed without a notification and throws ExpressionChangedAfterItHasBeenCheckedError. The guide marks it as developer preview, so keep it out of production.
  3. Click through every screen that shows live data. Lists after a save, counters, a button that should become enabled, anything that updates after a request. A stale screen is easy to miss in a test and easy to spot by hand.
  4. Look at your tests. TestBed is zoneless by default, and the guide recommends await fixture.whenStable() over fixture.detectChanges(). If a test only passes after you call markForCheck() on the fixture, the component is usually not fully reactive yet.
  5. Test server-side rendering separately. Without Zone.js, Angular can't tell when the app is stable. The router and HttpClient already report their work through PendingTasks. For your own async work that must finish before the page is rendered on the server, use PendingTasks too.

Remove Zone.js last

You don't need to remove Zone.js to fix any of the above. When the app behaves, take zone.js and zone.js/testing out of the polyfills in the build and test targets of angular.json, then run npm uninstall zone.js. Leave existing NgZone.run and runOutsideAngular calls alone: the guide warns that removing them can slow down libraries that still rely on Zone.js. Make this its own release, so a problem points to one cause. I describe the upgrade order in Angular 20 end of support: planning the move to Angular 22.

If speed was the reason

Fewer change detection runs can make a page feel faster, but don't assume it. Measure before and after, the way I describe in Measure web performance before you optimise. For a full example of budgets, server rendering and caching on an Angular site, see Core Web Vitals on an Angular SSR site.

I build and maintain Angular frontends on ASP.NET Core as part of custom web application development. If an upgrade left your application showing stale data, send me the Angular version and a short description of the screen and I will tell you where I would look first.

Sources

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.