In Web Development
Angular 14 might change the way we write Angular components! - read the full article about Angular Js update, Web Development and from Academind on Qualified.One
Angular version 14 was released and like every new version, it, of course, also introduces a couple of smaller changes and fixes, and minor new features and improvements, and Ill provide a link to the official announcement blog post below the video, attached to this video so that you can read and learn all about those smaller changes.
However, it also introduces two big new features: Typed Forms and Standalone Components, though the latter is in developer preview and might change until it is fully stable in a future Angular version.
But in this video, well take a look at these two big new features.
Ill let you know what this means for Angular and your apps, and Ill also let you know what this means for my Angular course in case youre taking that.
Now, lets start with Typed Forms, the first new bigger feature introduced by Angular 14.
Here Ive got a standard form using the Reactive Forms approach, which is offered by Angular.
In Angular, you can handle forms with the template-driven approach or by using the Reactive approach, and were using the Reactive approach here because Typed Forms, this new feature, only applies to Reactive Forms, not to Template-driven Forms.
Theres no change to Template-driven Forms.
Now, here for Reactive Forms though, the HTML code is the same you would have written for Angular 13, 12, 11 and so on.
So this does not change.
If you have a look at the TypeScript code for this component, the code also doesnt really change that much.
Im creating a FormGroup here with an email and a age FormControl and Im connecting this FormGroup and then the individual controls to my HTML elements.
Thats what Im going here.
However, well see a difference if we then try to work with our FormGroup here in the TypeScript code.
For example, upon form submission, we might want to log or send the individual form-controlled values to a server or to the console here in this case.
Therefore, we can access this FormGroup and what youll notice now is that if I try to access the values of this form with the value property on the form FormGroup, if I add another dot, Im getting auto completion here, which basically tells me that I only have the age and email value here.
Now, prior to version 14, we would have not gotten any auto completion here because Angular didnt know that this FormGroup only has these two FormControls because FormGroup was basically of type any before.
So it didnt know what was inside of it.
This changed now.
So therefore, if I, for example, try to access a password control here, I get an error in my code because now Angular knows that such a FormControl does not exist in this FormGroup, and I have to add it first or fix my error in another way.
But its not just the existence of FormControls, which is now known to Angular.
Its also the type of value that can be entered into a FormControl.
For example, here I could try to output age and I might want to add plus one to it.
Now, if I do this, then Im actually getting an error here because this could be null or undefined.
We can, of course, let Angular know that this will never be null by adding an exclamation mark but again, we now have to be a bit more explicit and we can avoid unintended errors where we accidentally might operate on a null value.
And of course here adding the exclamation mark would actually not be a great solution because we dont know if this is null or not.
But its not just the null check, which we now might need to add to our code to avoid accidental work on null values.
Its also more than that because now we just dont know whether a control exists or not and whether its maybe null or not, and we have to add code to make sure that were not operating on null values.
We now also have extra information on the type of value we have when its not null.
For example, I might want to count my email characters for whatever reason.
I want to know the length of characters entered in the email address.
Therefore, I could add an exclamation mark to let TypeScript know that I know that this will never be null.
The better solution, of course, probably would be to add some guard here though where I check if this form email is set.
So if its not null, and then I operate on it.
But either way, I can make sure that I know that this wont be null and then I could try to access length here to get the number of characters entered.
But now Im getting another error because thats the last piece you should know about Typed Forms.
Its not just about null values and the existence of FormControls and FormGroups, its also about the type of value that can be entered into a FormControl.
And the way Im initializing the FormGroup here basically tells TypeScript that I have two controls in that FormGroup and theyre both null initially but Im not passing any other information to TypeScript and therefore, TypeScript infers that they are always null so that there can never be any other value inside of such a FormControl besides null.
And this is probably a bit too strict because, of course, I might want to initialize a FormControl to be null initially, to have no value initially but eventually, the user should be able to enter a value and then, of course, that will not be a null value.
To let TypeScript know which value might eventually end up in such a FormControl, FormControl is now a generic type where we can specify which values can end up in that FormControl.
And here we could say that its a string or null.
Null initially but once the user entered something, a string.
For age, we could say that its maybe a number or null.
That would be possible here.
We then, of course, wanna make sure that the type is set to number here on our input field.
And now with that, you see this error for the email went away because Im telling TypeScript that if this is not null, if its set to some value, it will be a string because that is what Im clarifying here.
And the idea behind Typed Forms is simply that you have more type safety in your TypeScript code, that youre not operating on any as much because the problem with type any is that all your code looks fine whilst youre writing it but your app could crash in certain scenarios when its actually running, so at runtime and you typically wanna avoid that.
So thats one new feature.
Now, one important question, of course, is what does this mean for your existing apps? Well, if youre not using Angulars strict mode, then it actually doesnt have any impact because you have way less stricter type checks in that case anyways.
If your Angular app was initialized using strict mode though, then you might need to update your code and add extra generic type annotations like this or extra type guards like this where you make sure that you only operate on a value that could be null after checking whether it is null.
If you dont have the time to update all your code like this right now, if you dont wanna add all these extra annotations right now because it would mean a lot of work for you, you can also follow an alternative approach and instead of FormControl, you could use the UntypedFormControl and also the UntypedFormGroup.
This basically gives you back those old FormControls and old FormGroups, which you had in Angular 13 and earlier, and all your code will then continue to work as it did before, and you see this error, for example, is gone.
And if youre using the ng update command provided by the Angular CLI, it should actually make those replacements for you automatically.
This allows you to then switch back to FormGroup and FormControl and add those extra type annotations whenever you have the time for it.
Now, Typed Forms, Typed Reactive Forms to be precise are one nice feature, which can improve the overall type safety of your application.
Another big new feature, especially for the future of Angular are Standalone Components though.
Standalone Components is one of the most amazing features added to Angular in a long time because it has the potential of making writing Angular apps much simpler and easier than it is today.
The idea behind Standalone Components is that you can get rid of NgModules.
So of these extra module files with all those imports and with all the extra declarations you have to add there because at the moment with Angular 13 and older, when you add a new component, you have to add it to declarations in the NgModule, and if that component wants to use certain directives or pipes, you have to add the appropriate import to the imports array.
If you want to use dependency injection, you have to add the services to providers and so on.
Now, there is a reason why this was required in the past but with Angular 14, we now get the developer preview of a new way of writing components, components without NgModules, Standalone Components.
Now, developer preview means that it might change, that its not fully stable yet and thats why you maybe dont wanna switch to this feature entirely yet but for the future of Angular, it might be a huge step forward and it might change the way we write Angular apps, making it much easier to write Angular apps.
So how are Standalone Components used then? Well, lets say we wanna migrate the AppComponent to be a Standalone Component without requiring this NgModule.
To make this work, we have to add a new flag to the @Component decorator configuration here, the standalone flag, and we have to set this to true.
Now, I would imagine that maybe in the future, this flag goes away or is changed again but at the moment, in this developer preview with Angular 14, we have to add the flag like this.
After adding this flag here, we have to add all the imports and providers that are required by that component to this component.
Now, this component here doesnt need any providers but it needs some imports because I am, for example, using forms in there.
Therefore, I should add the ReactiveFormsModule as a import here.
So the features exposed by that built-in module can now still be used in this component.
If this component would be using a bunch of other components, those components would also be added to the imports array here.
So there is no declarations array which we add here, as we have it in NgModule, but instead, if this component used other components, they would also be added to imports here.
So imports, in the Component decorator basically specifies all the external dependencies, modules, pipes, other components, directives and whatever used by this component here.
Now, after adding this flag and the dependencies of a component to the component, you can get rid of the module that existed for this component.
In this case, the AppModule file and now since this was the AppModule, our root module for the root component, we also have to bootstrap the application differently.
Instead of bootstrapping it like this, as we did it in the past, we now get rid of this AppModule import here and we bootstrap by calling bootstrapApplication, which is imported from @angular/platform-browser.
We can get rid of the platform-browser-dynamic import here.
bootstrapApplications a function, which is called and to this function, we now pass the root component of our application, in this case, the AppComponent, which therefore also has to be imported from the AppComponent file.
This is how we now bootstrap our application if our root component is a Standalone Component.
And as a side note, you dont have to migrate all your components to Standalone Components.
If you have an application where you wanna try this feature, you can mix and match Standalone Components with modules just as needed.
And again, it is in developer preview anyways, so well have to see how this feature evolves and which patterns will evolve.
With that, however, were bootstrapping this.
And if we now run ng serve again, this application will be bundled up and we can still visit it in the browser, use it as we did before, submit this and have this app work.
At the point of time Im recording this video, Im getting some errors here in the HTML template because my extension, my VS Code extension doesnt yet understand what this component has access to the Reactive Forms module but it should only be a matter of time until this goes away.
Technically, this code is correct and this is how you can use Standalone Components to make your components more independent and get rid FormGroup a bunch of boilerplate code.
Now, this is one of the features Im personally most excited about because this has, as mentioned before, the potential of making Angular apps much simpler and getting rid of a lot of unnecessary or extra code, which we had to write in the past.
I wanna emphasize again that it in developer preview, so well have to see how it evolves and what this means for the future of Angular but it has great potential.
And thats it for the two most exiting features Angular 14 has to offer.
Typed Forms can help us make our code safer and catch errors earlier.
Migrating should be easy because you can go back to the old forms until youre ready and you only have to add a bunch of typed notations and fix some errors you might have in your code whilst migrating.
Now, Standalone Components are not fully there yet.
Its in preview, and the feature might still change in certain aspects and some code might change, so its not something I would recommend using for all your components and all your applications yet but as mentioned, it is a feature Im super excited about and it has the potential of making Angular way more awesome and allowing us to build way leaner Angular apps in the future.
So Im super happy to see this and Im very excited to see how that will push Angular forward.
Now, what does this mean for your apps and for my course in case youre taking that? Well, your apps should be easy to update.
You dont have to use Standalone Components and maybe you shouldnt yet for all your components.
It is a feature you should play around with, not necessarily migrate everything to.
And for Typed Forms, as mentioned, updating should be fairly easy and it will help you make your code safer and you can go to the untyped formed for the time you need to be ready to update if you dont wanna update just yet.
So updating shouldnt be too hard.
Now, when it comes to my course, that course is still teaching you the latest version of Angular because the main syntax has not changed.
Now, I will definitely keep a close eye on Standalone Components because I, of course, do plan to update this course and bring it up to date with this amazing new feature once it is stable and once its clear whether that really is the way forward when it comes to building Angular applications and components.
Maybe in the future we only only build Angular apps with Standalone Components and we dont really need Angular modules at all anymore.
Its too early to tell but Ill keep an eye on that and of course, Ill make sure that my course stays updated and youll learn the latest version of Angular with the course, and you learn the correct way of writing Angular apps.
Academind: Angular 14 might change the way we write Angular components! - Web Development