🚨 Angular 18 Is Out: Zoneless Upgrade (Step-By-Step)

Angular University
23 May 202414:35

Summary

TLDRThis video tutorial guides viewers through the process of upgrading an Angular 17 application to Angular 18, focusing on the new experimental zoness change detection. It demonstrates how to enable zoness, compares its behavior to traditional zones, and offers solutions for typical change detection scenarios. The video also highlights additional features in Angular 18, such as default fallback content for NG content slots, a new forms observable for reactive forms, and enhanced routing capabilities with function support in redirectTo. Finally, it introduces a new version of Angular Material compatible with server-side rendering and client hydration.

Takeaways

  • πŸš€ Angular 18 has been released with initial support for Zones.js.
  • πŸ”„ The video demonstrates how to migrate an Angular 17 application to Angular 18 and enable Zones.js.
  • πŸ“ˆ The presenter compares the behavior of an application using Zones with a new Zones.js Angular 18 application.
  • πŸ› οΈ To migrate to Zones.js, one must first upgrade to Angular 18 and then modify the application configuration to use experimental Zones.js change detection.
  • πŸ” The global Zone object's presence confirms the use of Zones in an application.
  • βœ… After migration to Angular 18, the application still works as expected because it continues to use Zones until explicitly switched to Zones.js.
  • πŸ”„ Zones.js change detection in Angular 18 is marked as experimental and has known limitations, but is sufficient for many applications.
  • πŸ‘€ The video shows that after switching to Zones.js, the global Zone object is no longer defined, indicating a Zones.js environment.
  • πŸ”§ The video covers how to handle change detection in Zones.js, particularly with event handlers and observables, and the use of ChangeDetectorRef as a workaround.
  • πŸ“š It is recommended to use signals with Zones.js change detection for a more practical approach than using ChangeDetectorRef.
  • 🌟 Additional features in Angular 18 include default fallback content for NG content slots, a new forms observable for reactive forms, and enhanced route configuration with function support in redirectTo.
  • πŸ’‘ The video also mentions a new version of Angular Material compatible with server-side rendering and client hydration.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about migrating an Angular 17 application to Angular 18 and turning on the new Zones.js support called 'zone.js'.

  • What is the initial form of zone.js support called in Angular 18?

    -The initial form of zone.js support in Angular 18 is called 'zoness'.

  • How can you confirm if an Angular application uses zones?

    -You can confirm if an Angular application uses zones by checking for the presence of the global Zone object.

  • What command is used to upgrade an Angular application to version 18?

    -The command to upgrade an Angular application to version 18 is not explicitly mentioned in the script, but it implies using Angular's CLI commands to perform the upgrade.

  • What is the recommended approach to handle change detection in a zoness Angular application?

    -The recommended approach to handle change detection in a zoness Angular application is to use signals, which automatically inform Angular when data changes.

  • Why is using ChangeDetectorRef not recommended for zoness change detection?

    -Using ChangeDetectorRef is not recommended for zoness change detection because it's not practical to use it everywhere in your application, such as after every HTTP call.

  • What is the new feature in Angular 18 regarding NG content projection?

    -The new feature in Angular 18 regarding NG content projection is the ability to have default fallback content for NG content slots.

  • What is the new observable feature for reactive forms in Angular 18?

    -In Angular 18, there is a new 'events observable' feature for reactive forms that allows developers to subscribe to and react to all the events happening in the form.

  • How has the 'redirectTo' configuration changed in Angular 18 for routing?

    -In Angular 18, 'redirectTo' can now take a function instead of just a string, allowing for dynamic redirection based on parameters or query parameters in the URL.

  • What is the latest version of Angular Material mentioned in the video, and what does it support?

    -The latest version of Angular Material mentioned in the video is compatible with server-side rendering with client hydration and follows the Angular Material free specification.

Outlines

00:00

πŸš€ Angular 18 Migration and Zoness Introduction

The video script introduces Angular 18, highlighting its new feature, Zoness, which is an experimental change detection mechanism. The host, Vasco, guides viewers through the process of upgrading an Angular 17 application to Angular 18 and then enabling Zoness. The script explains how to confirm the use of Zones in an application by checking for the global Zone object and demonstrates simple change detection scenarios, such as a plain counter, an interval counter, and an observable counter. The migration process involves upgrading to Angular 18, modifying the application configuration to use Zoness change detection, and removing the Zone JS polyfill. The video also covers the necessary steps to restart the Angular CLI to apply the new version of angular.js. Finally, it confirms that the application is running as Zoness by checking for the absence of the global Zone object.

05:01

πŸ” Exploring Zoness Change Detection and Signal Usage

This section of the script delves deeper into how Zoness change detection works in practice. It compares the behavior of different change detection scenarios under Zoness, such as event handlers, observables, and interval counters. The script shows that while event handlers and observables trigger change detection as expected, interval counters do not update the template unless explicitly notified. To address this, the script suggests using the Change Detector Ref to mark the component for check, although it recommends against this approach due to its impracticality. Instead, it advocates for the use of signals, which are a more practical and recommended solution for Zoness change detection. The script demonstrates how to refactor an interval counter into a signal and update it, ensuring that changes are reflected in the user interface without needing to use Change Detector Ref.

10:03

πŸ› οΈ Additional Features of Angular 18 and Upcoming Courses

The final paragraph of the script covers additional features introduced in Angular 18. It starts with NG content and content projection, where a new feature allows for default fallback content in NG content slots. An example is given with a course card component that projects a title using NG content. The script then moves on to discuss a new observable for reactive forms in Angular, which allows developers to subscribe to and react to all form events. Next, it introduces a new function in 'redirect to' for route configuration, enabling more dynamic redirection based on parameters. The script wraps up with the release of a new version of Angular Material, which is compatible with server-side rendering and client hydration, and follows the Angular Material free specification. The host also promotes his upcoming course on modern Angular with signals, which will cover building Angular applications from scratch using this new approach.

Mindmap

Keywords

πŸ’‘Angular 18

Angular 18 is the latest version of Angular, a popular open-source web application framework led by the Angular Team at Google and by a community of individuals and corporations. In the video, Angular 18 is significant as it introduces new features and improvements, such as the initial support for 'Zoneless' change detection, which is a major update from the previous versions.

πŸ’‘Zoneless

Zoneless, often abbreviated as 'zoness' in the script, refers to a new feature in Angular 18 that allows applications to run without the need for Zone.js, a library used by Angular for change detection and other asynchronous operations. The video demonstrates migrating an Angular 17 application to Angular 18 and turning it into a Zoneless application, showcasing the benefits and necessary steps involved.

πŸ’‘Change Detection

Change detection is a core mechanism in Angular that checks for changes in data-bound properties and updates the view accordingly. The video discusses how change detection behaves differently with Zones and Zoneless, and how to handle various scenarios like click events, intervals, and observables under Zoneless change detection.

πŸ’‘Migration

Migration in the context of the video refers to the process of upgrading an existing Angular application from one version to another, specifically from Angular 17 to Angular 18. The script outlines the steps needed to perform this migration, including updating the application's configuration and dealing with potential changes in behavior.

πŸ’‘Angular CLI

Angular CLI is a command-line interface for Angular that facilitates the creation, development, and maintenance of Angular applications. The script mentions the use of Angular CLI commands to perform the migration to Angular 18 and to confirm that the application is running on the new version.

πŸ’‘Observable

In Angular, an Observable is a core RxJS (Reactive Extensions for JavaScript) concept that represents a stream of values. The video script uses an 'observable counter' as an example to demonstrate how data changes are pushed to the template and how they behave under Zoneless change detection.

πŸ’‘Signal

A signal, as discussed in the video, is a recommended approach for handling asynchronous data changes under Zoneless change detection. It is a way to encapsulate a value and notify subscribers about its changes. The script suggests using signals instead of directly manipulating the ChangeDetectorRef for better practice.

πŸ’‘ChangeDetectorRef

ChangeDetectorRef is a service provided by Angular that allows manual intervention in the change detection process. In the script, it is mentioned as a way to force change detection when using intervals under Zoneless, but the video recommends using signals instead for a more practical and clean solution.

πŸ’‘Content Projection

Content projection is a technique in Angular that allows components to include and display external content via the `<ng-content>` tag. The video introduces a new feature in Angular 18 that provides default fallback content for NG content slots, enhancing the flexibility of component design.

πŸ’‘React Forms

React Forms is a library in Angular that provides a model-driven approach to form handling. The script mentions a new feature in Angular 18 that introduces an 'events observable' for react forms, allowing developers to subscribe to and react to all form events.

πŸ’‘Redirect

Redirect is a routing feature in Angular that allows navigation to a different route. The video script highlights a new capability in Angular 18 where the 'redirectTo' property in route configuration can now use a function to determine the redirect path dynamically, adding flexibility to routing logic.

πŸ’‘Angular Material

Angular Material is a UI component library for Angular applications, providing a set of reusable, well-designed components. The script mentions a new version of Angular Material that is compatible with server-side rendering and client-side hydration, aligning with the 'Angular Material free' specification for improved performance and user experience.

Highlights

Angular 18 has been released with initial support for Zoness.

The video demonstrates migrating an Angular 17 application to Angular 18 and turning on Zoness.

It compares the behavior between a Zones-based application and a new Zoness Angular 18 application.

The tutorial shows tricks needed to migrate to Zoness.

All extra features available in Angular 18 are covered.

Migrating to Zoness is easier than it sounds.

The presence of the global Zone object confirms the use of Zones.

Three simple change detection scenarios are created: a plain counter, an interval counter, and an observable counter.

Upgrading to Angular 18 involves updating package.json and using the application Builder.

After migration, the application still works as before because it's still using Zones.

To turn the application into a Zoness application, modifications are needed in the application configuration.

Zoness change detection is marked as experimental with known limitations.

The global Zone object is no longer defined in a Zoness application.

Zoness detects click handlers and triggers change detection runs.

Observable counters work correctly with Zoness when using the async pipe.

Interval counters do not update in the template unless fed through the async pipe or similar.

Injecting ChangeDetectorRef and using MarkForCheck can fix the issue with interval counters.

Using signals is recommended over ChangeDetectorRef for Zoness change detection.

Angular 18 introduces default fallback content for NG content slots.

A new forms observable for reactive forms in Angular 18 allows subscribing to form events.

Angular 18 allows the use of a function in redirectTo for route configuration.

A new version of Angular Material compatible with server-side rendering and client hydration is available.

Angular University offers courses on modern Angular with signals.

Transcripts

play00:00

angular 18 is out now and the support

play00:03

for zoness is available in its initial

play00:06

form in this video I'm going to show you

play00:08

how to take an angular 17 application

play00:10

and migrate it to angular 18 turn on

play00:14

zoness compare the difference of

play00:17

behavior between an application based on

play00:19

zones and a new zoness angular 18

play00:23

application show you the tricks that you

play00:25

need to know to migrate to zoness and

play00:28

I'm also going to cover all all the

play00:30

other extra features that are made

play00:32

available in angular

play00:34

[Music]

play00:38

18 welcome back to the angular

play00:40

University Channel I'm Vasco so as

play00:43

you're going to see migrating to zoness

play00:45

is way easier than it sounds let's take

play00:48

here this plain angular 17 application

play00:51

that is currently using zones and let's

play00:54

confirm that indeed it uses zones and

play00:57

after that we're going to migrate it to

play00:59

Zone L and angular 18 so to make sure

play01:02

that your application uses zones just

play01:04

check for the presence of the global

play01:06

Zone object as you can see this

play01:08

application is Zone based I'm not using

play01:11

here on push change detection I have

play01:14

created here free simple change

play01:16

detection scenarios a plain counter an

play01:20

interval counter that is being updated

play01:22

here via set interval and an observable

play01:26

counter that is being pushed to the

play01:28

template via an observable so if we

play01:31

check here the template we are printing

play01:32

out the plane counter the plane counter

play01:35

will increment when we click on this

play01:37

increment plane counter Button as you

play01:39

can see here the interval counter is

play01:42

going to be incremented every second as

play01:46

you can see here via set interval and

play01:49

finally the observable counter is being

play01:52

pushed to the template using the sync

play01:54

pipe so let's Now quickly migrate this

play01:57

to zoness and compare what happens in

play02:00

this free typical change detection

play02:03

scenarios in order to migrate to zoness

play02:06

First upgrade to angular 18 using the

play02:08

following command this is going to take

play02:10

a moment to process I will return When

play02:13

the installation is completed all right

play02:15

so a few updates were made in

play02:16

package.json and now we are being asked

play02:19

the question use the application Builder

play02:21

I'm going to hit space to say that yes I

play02:24

want to use it and this is going to run

play02:28

here a couple of migration

play02:30

and now this application is running on

play02:33

angular 18 to confirm this let's go

play02:36

ahead and let's hit NG serve and this is

play02:39

going to build the application that as

play02:42

we can see is now running here on

play02:45

angular 18 and everything is still

play02:47

working as before as expected because we

play02:51

are still using zones so just because

play02:54

you migrate to angular 18 does not mean

play02:57

that you are using zoness let's not now

play03:00

modify our application and turn it into

play03:02

a zoness application for that we need to

play03:05

do a couple of modifications so first

play03:08

let's go here to our application

play03:10

configuration and here in your providers

play03:13

you're going to add the following

play03:14

provider provide experimental zoness

play03:18

change detection now the zoness change

play03:20

detection is marked as experimental I'm

play03:23

going to talk about a bit the currently

play03:26

known limitations but I think that for a

play03:29

lot of applic ations this initial

play03:31

version of zoness will be more than

play03:34

enough the next step is to head over to

play03:37

angular do Json and here we are going to

play03:40

search for the Zone JS polyfill and

play03:43

we're going to go ahead and we're going

play03:45

to remove it now the last step and this

play03:48

is very important you need to stop your

play03:50

angular CLI and you need to run it again

play03:53

otherwise the new version of angular.js

play03:56

would not be applied to your application

play03:59

now let's confirm that this is indeed

play04:02

zoness I'm going to head over here to

play04:04

the console and let's try to print out

play04:07

just like we did before the global Zone

play04:09

object and this time around we have here

play04:12

zone is not defined so this application

play04:15

here is effectively zoness so let's have

play04:19

a look to see what happened you might

play04:22

have here the illusion that everything

play04:23

is still working like before that is not

play04:26

exactly the case so let's simplify this

play04:29

and let's go each use case at a time

play04:32

let's start here with the plane counter

play04:35

so as you can see when you increment the

play04:37

plane counter this is still working

play04:40

correctly as expected so what is

play04:42

happening here is that zoness detects

play04:44

that a click Handler is clicked on a

play04:47

button and that is going to trigger a

play04:50

change detection run so in that sense

play04:53

everything works like default change

play04:55

detection or on push this has always

play04:58

worked in all change detection modes all

play05:01

sorts of event handlers that are binded

play05:04

here in the angular template using the

play05:07

rounded parenthesis syntax will trigger

play05:10

change detection as usual also in zoness

play05:14

now let's have a look at the behavior

play05:16

here of our observable counter so I'm

play05:19

going to move this up and we are going

play05:21

to uncomment this and see the behavior

play05:24

so as you can see after application

play05:26

refresh the observable counter is still

play05:29

working correctly as expected so the

play05:32

fact that we are using the a sync pipe

play05:35

to push data to the template that is

play05:38

going to trigger zoness change detection

play05:41

or in other words it's going to inform

play05:44

angular that some data has changed in

play05:47

this component and this component has

play05:49

been marked as dirty and angular knows

play05:53

that it needs to update it all right so

play05:55

so far so good this pretty much works

play05:57

like default change detection and even

play05:59

on push this is standard behavior of

play06:01

angular now let's go ahead and let's

play06:04

carefully comment out this interval here

play06:08

to avoid having our application

play06:11

continuously triggered change detection

play06:13

due to this interval and let's inspect

play06:15

this interval counter separately all

play06:19

right so what is going on here so as you

play06:22

can see this interval counter even

play06:25

though it's currently being updated

play06:28

every second and incremented this is not

play06:32

getting reflected here in our template

play06:35

so because this interval counter is not

play06:38

being fed to the template using

play06:40

something like the sync pipe there is no

play06:43

way for zoness change detection to know

play06:46

that this counter is being changed the

play06:49

same thing would happen with set timeout

play06:52

for example there would also be no way

play06:56

for zoness change detection to know that

play07:00

a value has been changed in those

play07:02

circumstances as well so how could we

play07:05

fix this well we can head over here to

play07:08

our zoness application and we need to

play07:12

somehow here when we increment our

play07:15

counter we need to notify angular and

play07:17

inform it that a change has happened in

play07:20

the data of this component so I'm going

play07:22

to go ahead and I'm going to inject here

play07:25

the change detector ref and with it it

play07:30

we are going to go ahead and we're going

play07:32

to notify angular so I'm going to access

play07:36

here change detection ref and we're

play07:39

going to say here Mark for check and now

play07:42

this time around if we try this we're

play07:44

going to see that the counter is being

play07:47

correctly updated in the user interface

play07:49

as expected Now using change detector

play07:52

ref might sound like a clever trick but

play07:55

I don't recommend this approach I think

play07:57

that instead it's much better to use

play07:59

signals because it's really not that

play08:02

practical to use change detector ref

play08:04

everywhere in your application imagine

play08:06

that you have to call this after every

play08:09

HTTP call well you could put it in an

play08:11

Interceptor Etc but I'm going to show

play08:13

you what I think is a better way which

play08:16

is to instead use signals so let's go

play08:19

ahead and let's turn this interval

play08:22

counter into a signal let's provide it

play08:25

its initial value and here let's

play08:28

refactor this to update the signal so

play08:31

it's going to be counter equals counter

play08:34

+ one so now we have performed here the

play08:37

update of the counter and now don't

play08:39

forget head over here to the template

play08:43

and this is now a signal you have to

play08:46

invoke it otherwise this won't work as

play08:49

expected and this time around as

play08:51

expected without having to resort to

play08:54

change detector rev you have here the

play08:57

interval counter working correctly as

play08:59

expected and this is in general the

play09:02

recommended solution for working with

play09:04

zoness change detection which is to

play09:07

prefer to use signals so for example

play09:10

when you are doing an HTTP call and you

play09:13

want the data after the HTTP call to be

play09:15

reflected in the user interface push the

play09:18

data into a signal and consume that

play09:20

signal in your templates and everything

play09:23

will work perfectly and that was the

play09:26

main new feature of angular 18 zoness

play09:29

change detection in an initial version

play09:32

now let's with Rapid Fire cover the

play09:34

other features that you have available

play09:36

going on with NG content and angular

play09:40

content projection you now have default

play09:43

fallback content for your NG content

play09:46

slots so let me show you here an example

play09:49

I've created here a course card

play09:51

component that takes in here as a

play09:54

configurable NG content input a title in

play09:57

this case an H2 tag with the class title

play10:00

and we can see the course card printed

play10:03

out here to the screen now let's have a

play10:06

look here at the content we can see the

play10:09

component and we are using here NG

play10:11

content we are using here a Content

play10:13

projection slot we are currently

play10:16

checking here for an element in the

play10:19

content part with the class title and

play10:22

then we are projecting it here at this

play10:26

place in the template now one thing that

play10:28

we did not have so far was the

play10:30

possibility of defining here default

play10:34

content in case that we chose not to

play10:37

provide here any initial value for the

play10:41

title projection slot but now this is

play10:44

working so as you can see we now have

play10:47

default content available for NG content

play10:51

you might have noticed here this error

play10:52

message please disregard this it's just

play10:56

that webstorm is not fully up to date

play10:59

yet

play10:59

and you might be curious why does this

play11:02

look like Visual Studio code well I know

play11:05

that I'm going to get that question in

play11:07

the comments so I'm using the visual

play11:09

studio code dark modern theme so you get

play11:12

the best of both worlds you get webstorm

play11:15

with the beautiful vs code theme now

play11:19

let's go ahead and let's cover our next

play11:22

feature of angular 18 which is going to

play11:25

be the new forms observable for react

play11:29

forms so let's say that you have here a

play11:32

reactive form in your application you

play11:34

now have here this new events observable

play11:38

that you can subscribe to and this is

play11:40

going to report all the events that are

play11:44

happening in the form so let me show you

play11:46

here I'm going to open here another Tab

play11:50

and let me go ahead and let me load the

play11:53

application let's quickly use here the

play11:56

console and let me go ahead and let me

play11:59

open open here the form and from here

play12:03

I'm going to start creating here some

play12:07

events in the form we can see that

play12:08

already here some events occurred so

play12:11

such as for example when the value was

play12:13

initially set when the form became valid

play12:16

and now if I select here another value

play12:19

we have a touch change event we have

play12:22

here another event for example when we

play12:27

provide here another value here for this

play12:30

input we can see here all the events

play12:33

that are currently being fired

play12:36

internally in our form we can subscribe

play12:39

to them and react to them using these

play12:42

new events observable next up and the

play12:46

final feature that we're going to cover

play12:48

or one of the final features the

play12:51

possibility of using a function here in

play12:54

redirect two when configuring routes in

play12:58

angular 18 so before redirect to could

play13:01

only take a string now if you want to

play13:04

redirect based on some parameter such as

play13:08

for example on a router parameter or

play13:10

even a query parameter in the URL you

play13:13

can return here a string with the URL

play13:16

that you want to redirect to in our case

play13:19

we have here the lessons menu entry that

play13:22

is going to be redirected to the login

play13:25

page and finally to complete our

play13:28

coverage of of angular 18 we have

play13:31

available here a new version of angular

play13:35

material that is compatible with server

play13:39

side rendering with client hydration and

play13:42

that follows the angular material free

play13:45

specification so you will notice that

play13:47

the components are slightly different

play13:50

than before and if you want to learn

play13:53

angular in detail check out my courses

play13:56

at the angular University I'm currently

play13:59

finishing up my latest course which is

play14:01

the modern angular with signals course

play14:04

that you can find here already many

play14:07

hours of content are here available even

play14:10

though the course is not fully completed

play14:13

this covers how to build a modern

play14:15

angular application from A to Z using

play14:19

signals and with this I hope that you

play14:22

enjoyed this coverage of angular 18 let

play14:25

me know in the comment section below

play14:27

what you think about this release if if

play14:29

you are planning to migrate to zoness

play14:31

thank you so much for watching everyone

play14:33

and I will see you next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Angular UpdateZones MigrationZoness CDWeb DevelopmentUpgrade GuideChange DetectionAngular 18Reactive FormsNG ContentSignal Usage