Send your request Join Sii

The Angular team is dedicated to releasing a new major version every six months. The latest version, 17, was released on November 8, 2023.

Version 17 includes several new features, which I will introduce in the article, including:

  • New Control Flow,
  • Deferred Loading,
  • Support For View Transitions API,
  • Support for TypeScript 5.2.

New Control Flow Syntax

The new control flow, in my opinion, is the most crucial change in this release. After a long debate and consultation with the Angular community, the team is introducing a completely new @ – syntax.

It provides the functionality of structural directives *ngIf, *ngFor, *ngSwitch with some extra features. Let’s dive into some examples.

Example #1 – NgIf with the else option and the new syntax

code

As we can see, the old way of displaying some info up to some predicate was to use a *ngIf structural directive. The else block has to be implemented using another ng-template, and reference to that was set after the else keyword.

The new syntax gives us a plain if-else control without adding extra templates.

Example #2 – Empty with @for loop

With the new syntax comes a new feature – handling empty lists! Previously, to handle an empty list with proper information for the user, we had to create a separate ngTemplate and extra ngContainer to bind the if statement with its else option.

With the new syntax, we can do the same more shortly by declaring @empty right after for block scope. It’s also worth mentioning that, in the past, when using the ngFor directive, we had the option to utilize a trackBy property and a recognition function to optimize rendering. The new syntax simplifies this process by requiring only the property name the track function will use for object recognition.

code

Example #3 – Switch with @switch

Switch syntax in a new version is very similar to the old implementation of NgSwitch.

code

Here are a couple of reasons why the new flow syntax has been introduced.

The previous implementation of the control flow was based on the zone-dependent application. Structural directives are strongly coupled with Change Detection mechanisms. Angular Team has decided to go in the direction of a zoneless application with a Signals implementation, and now it’s making another step towards it.

Therefore, the team has decided to create a new implementation of control flow, and the plan is to deprecate the current implementation of structural directives as ngIf and ngFor but not remove them “until the ecosystem (including community-authored content) has switched over to the new control flow.” The NG Team has prepared an auto-migration scheme to facilitate incorporating new syntax changes. The only command that needs to be run is:

ng g @angular/core:control-flow

Be aware that this schematic is still only a developer preview migration.

Deferred Loading

A defer block is The most exciting feature of a new control flow. The best way to show it’s magic is just by presenting an example.

Let’s assume we have a simple pipe that capitalizes the input.

code

By @defer block, the angular compiler knows it shouldn’t immediately import that pipe (if it’s not used elsewhere) while loading the component, but when the predicate is true.

The chunk of javascript, including upperCasePipe, is downloaded when we click the button.

giphy

We can see how our application is chunked after a successful build – Angular CLI will display how many chunks can be fetched.

lazy chunk files

What if the connection is slow or broken?

Angular handles that easily by providing possibilities to define the view during loading and in case of error.

code

For example, if we turn on an offline mode:

defer blocks
defer block example

But @defer control flow is much more powerful than it looks at first sight. We can connect the visibility of the component with some triggers.

code

Also, if we want to pass the responsibility to the browser, we can let the application manage to import code when a browser has finished its processes and is in an idle state.

code

This is particularly useful when you have heavy loadings that burden your page loading and user experience by slowing down features.

Difference between on and when

when: Some condition that triggers the change when it becomes true. After the change, it won’t revert to the previous condition even if the variable becomes false again.

on: Needs a predefined trigger that includes actions like interaction or entering the viewport. We’re able to combine multiple triggers.

Possible triggers:

  • idle – triggered when the browser reaches an idle state,
  • hover – triggered when the mouse has hovered over a specified area,
  • immediate – triggered without any waiting,
  • timer(x) – triggered after a specified amount of time,
  • viewport – triggered when a specified element comes into the viewport, in other words, when it’s visible to the user,
  • interaction – triggered when the user interacts with a specified element.

Why is it a crucial change? Advantages and disadvantages

There are a couple of reasons why this change is a vast improvement for the Angular apps.

Chunk loading

We could dynamically import the module’s code on demand by lazy loading modules. Right now, the Angular team has taken another step.

By importing a chunk of code on demand / idle / interaction, we can enhance user experience by fastening the page and loading the component.

Ease way of placing placeholders, loading view, and error handlers

The new syntax facilitates handling transition or failure states. Handling placeholders no longer needs other templates, interceptors, etc. Some problems with network traffic can also be handled with ease.

Support for view transitions API

View Transitions API is a feature that enables the creation of animated transitions between different DOM states. Until now applications handled animations, transitions by writing specific css and js files.
View Transition API is a new way of handling animations.

Full description of View Transitions API.

All major browsers now support the feature. IIn November 2023, View Transitions API was available in:

  • Chrome,
  • Edge,
  • Opera,
  • Samsung Browser.

Configuration in Angular is straightforward. All we have to do is configure the router during the bootstrapping application:

code

By providing this configuration, the default transition animation is visible:

Without view Transition API:

without view transition API

With View Transition API:

with view transition API

We can customize the way the animation is going, e.g.:

code

Support for TypeScript 5.2

Angular 17 comes with support for TypeScript 5.2. What is included in that?

The new use of keywords as a remedy for resource management

Using keywords can measurably help to handle resources. In a nutshell, it is a new block of code that declares a disposable resource. When the variable to which the resource was assigned is not used anymore, the method [Symbol.dispose]() is invoked, releasing the help.

Let’s take a look. We have some MockResourceHandler:

code

The old way of handling this resource would be to open it to try and finally block scope and to take closing connection in the final scope.

code

The new way requires changing the implementation of MockResourceHandler; the disposable interface and the [Symbol have to be implemented.dispose]() method has to be implemented as well.

code

Then, releasing resources will happen automatically on the method end:

code

Decorator Metadata

When using decorator functions, they gain access to a fresh metadata attribute within the object. Once all decorators have been executed within or attached to a class, the object can be reached within the class through the Symbol.metadata.

code

More details in ECMAScript proposal you can find on GitHub.

5/5 ( votes: 5)
Rating:
5/5 ( votes: 5)
Author
Avatar
Witold Drożdżowski

He has been working in software engineering for 5 years. At Sii, he serves as a Fullstack Developer, specializing in Java and Angular technologies. During his work, he places significant emphasis on the quality and performance of the products he creates. After hours, he enjoys designing applications from the UX/UI perspective and playing board games

Leave a comment

Your email address will not be published. Required fields are marked *

You might also like

More articles

Don't miss out

Subscribe to our blog and receive information about the latest posts.

Get an offer

If you have any questions or would like to learn more about our offer, feel free to contact us.

Send your request Send your request

Natalia Competency Center Director

Get an offer

Join Sii

Find the job that's right for you. Check out open positions and apply.

Apply Apply

Paweł Process Owner

Join Sii

SUBMIT

Ta treść jest dostępna tylko w jednej wersji językowej.
Nastąpi przekierowanie do strony głównej.

Czy chcesz opuścić tę stronę?