What's New Angular 19:

antoniocardenas

Antonio Cardenas

Posted on November 19, 2024

What's New Angular 19:

Angular is constantly evolving, and version 19 is no exception. With a focus on developer experience, improved performance, and new tools, Angular 19 offers features to streamline your projects and enhance efficiency. In this article, we’ll explore improvements in reactivity with signals, rendering optimizations, and much more. This marks the eleventh iteration since version 8, one of the first articles on this blog.

1. Standalone Components as the Default Standard

Standalone components, introduced in Angular 14, have been revolutionary, and now in Angular 19, they are the default behavior. This means you no longer need to declare standalone: true for each component.

Before Angular 19:

@Component({
  standalone: true,
  imports: [CommonModule],
  selector: 'standalone-component',
  templateUrl: './standalone-component.html',
})
export class StandaloneComponent { }
Enter fullscreen mode Exit fullscreen mode

Now:

@Component({
  imports: [CommonModule],
  selector: 'standalone-component',
  templateUrl: './standalone-component.html',
})
export class StandaloneComponent { }
Enter fullscreen mode Exit fullscreen mode

This eliminates the need for NgModules in most cases, simplifying application structure and promoting modular development. If you still need to work with NgModules, you can explicitly declare standalone: false.

2. Partial and Incremental Hydration for Faster Rendering

Angular 19 introduces partial and incremental hydration, transforming the performance of server-rendered (SSR) applications.

  • Partial Hydration: Prioritizes critical component loading, reducing initial interaction time.
  • Incremental Hydration: Defers feature loading based on user interactions (clicks or hovers), optimizing resource usage.

These techniques create faster, more interactive applications right from the start, improving the user experience.

Progresive Rehydration

3. Enhanced Signals: Simplifying Reactivity

Signals integration in Angular continues to improve, now with deeper support and new tools like LinkedSignal.

What are signals?

Signals are an API designed to manage states reactively and predictably, reducing reliance on Zone.js. This not only simplifies debugging but also improves performance and reduces bundle size.

Example with Signals: Introducing LinkedSignal

linkedSignal allows the creation of dependent signals that automatically update based on a source signal.

import { signal, linkedSignal } from '@angular/core';

const source = signal(10);
const double = linkedSignal(source, value => value * 2);

console.log(double()); // 20
source.set(15);
console.log(double()); // 30
Enter fullscreen mode Exit fullscreen mode

4. Improvements to Component API

Angular 19 introduces a new lifecycle hook, ngAfterSignalUpdate, to react after a Signal update, simplifying interactions with complex states.

import { Component, Signal, signal, AfterSignalUpdate } from '@angular/core';

@Component({
  selector: 'app-lifecycle',
  template: `
    <h3>Signal Lifecycle </h3>
    <button (click)="updateSignal()">Update</button>
  `,
})
export class SignalLifecycleComponent implements AfterSignalUpdate {
  mySignal = signal(0);

  updateSignal() {
    this.mySignal.set(this.mySignal() + 1);
  }

  ngAfterSignalUpdate() {
    console.log('Signal updated to:', this.mySignal());
  }
}
Enter fullscreen mode Exit fullscreen mode

This hook helps developers manage UI changes and complex state interactions effectively.

5. Faster Build Times

Thanks to optimizations in Angular CLI, builds are faster in both development and production environments. The addition of caching accelerates iterative processes.

Example: Build with Cache

ng build --optimization=true --build-cache
Enter fullscreen mode Exit fullscreen mode

6. Dependency Injection Enhancements

Angular now automatically infers types in Dependency Injection (DI), reducing repetitive code and increasing type safety.

Example: Type Inference in DI

// Before
constructor(private http: HttpClient) {}

// Now in Angular 19
constructor(private readonly httpClient) {}
Enter fullscreen mode Exit fullscreen mode

7. Resource and rxResource APIs: Simplifying Async Data

The new experimental resource and rxResource APIs are designed for promises and observables, simplifying asynchronous data handling.

Properties of resource:

  • Status: Tracks resource state (loading, success, or error).
  • Value: Contains data after loading.
  • Error: Handles errors.

Example with rxResource

import { rxResource } from '@angular/core';

const data = rxResource(async () => {
  const response = await fetch('/api/data');
  return response.json();
});

data.value.subscribe(data => console.log(data));
Enter fullscreen mode Exit fullscreen mode

These tools unify asynchronous data management, making workflows more intuitive.

8. Improved Routing

Angular 19 integrates signals more deeply with the router, enabling more reactive and streamlined navigation flows. This simplifies handling route and query parameters reactively.

Example: Using Signals with Router

import { Component, inject, OnInit, Signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-signal-route',
  template: `
    <h2>Signal Route</h2>
    <p>Route ID: {{ routeSignal() }}</p>
  `,
})
export class RouteDemoComponent implements OnInit {
  private route = inject(ActivatedRoute);
  routeSignal: Signal<string> = signal('');

  ngOnInit() {
    this.route.paramMap.subscribe(params => {
      this.routeSignal.set(params.get('id') ?? '');
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

Bonus: Angular Material Enhancements

Angular Material introduces new style improvements, accessibility features, and enhanced component interactions. For example, components like mat-menu and mat-select are now more intuitive and user-friendly.

Example: Improved mat-menu Usage

<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
  <button mat-menu-item>Item 1</button>
  <button mat-menu-item>Item 2</button>
  <button mat-menu-item>Item 3</button>
</mat-menu>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Angular 19 marks a significant step in the evolution of the framework. From default standalone components to improved reactivity with signals and SSR optimizations, this version provides tools to build faster, more reactive, and maintainable applications.

Ready to upgrade?

ng update @angular/core @angular/cli
Enter fullscreen mode Exit fullscreen mode

more info about update here:https://angular.dev/update-guide/?v=18.0-19.0&l=1

Share your experience with these new features and keep exploring the future of Angular development! 🚀, Stay tuned for upcoming articles deep down about Material, Signals and of course Hydration

¡Thanks to @damiansire for being the editor and approving this post!

💖 💪 🙅 🚩
antoniocardenas
Antonio Cardenas

Posted on November 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What's New Angular 19:
news What's New Angular 19:

November 19, 2024