Table of Contents
1. Angular Signals
- Server-Side Rendering
- Experimental Jest Support
- esbuild-Based Build System
- Required Inputs
- Router Inputs
- Standalone Project Support Other Features Related Blogs:-
Angular released a major version upgrade, Angular 16, on May 3, 2023. As an Angular developer, I found this upgrade interesting since there were some significant improvements compared to the previous version.
So, in this article, I will discuss the top 7 features of Angular 16 to give you a better understanding.
- Angular Signals Angular Signals is the main feature developers have been waiting for since the Angular 16 roadmap was released. Although Solid.js inspired this concept, it is a whole new concept for Angular. It allows you to define reactive values and express dependencies between them. In other words, you can efficiently use Angular signals to manage state changes within Angular applications.
A signal can be recognized as a regular variable that users can synchronously access. But it comes with some additional features like notifying others (component templates, other signals, functions, etc.) of its value changes and creating a derived state in a declarative way.
import { Component, computed, effect, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule],
template:
,
<h1>Calculate Area</h1>
<p>Answer : {{ area() }}</p>
<button (click)="calculateArea(10,10)">Click</button>
})
export class App {
height = signal(5);
width = signal(5);
area = computed(() => this.height() * this.width());
constructor() {
effect(() => console.log('Value changed:', this.area()));
}
calculateArea(height: number, width: number) {
this.height.set(height);
this.width.set(width);
}
}
In this example, I have created a computed value area and two signals named height and width. When the values of the signals are changed by invoking the calculateArea() function, the computed value will be updated and displayed in the template. Here is a working example for you to try it.
Although this looks fantastic, Angular has not abandoned zone.js and RxJS. Signals are an optional feature, and Angular will still work without them. Angular will gradually improve Signals in upcoming versions to make it a complete package.
- Server-Side Rendering The lack of server-side rendering (SSR) support was one of the most significant drawbacks of Angular compared to React. Angular 16 has resolved this issue with some significant improvements for server-side rendering.
Before, Angular used destructive hydration for SSR. In destructive hydration, the server first renders and loads the application to the browser. Then, when the client app gets downloaded and bootstrapped, it destroys the already rendered DOM and re-renders the client app from scratch. This approach caused significant UX issues, like screen flickering, and negatively impacted some Core Web Vitals such as LCP or CLS.anug.
Angular 16 introduces a new approach called non-destructive hydration to prevent these drawbacks. The DOM is not destroyed when the client app is downloaded and bootstrapped. It uses the same DOM while enriching it with client-side features like event listeners.
Non-destructive hydration is still at the developer preview stage. But you can enable it by adding provideClientHydration() as a provider when bootstrapping the application.
import {
bootstrapApplication,
provideClientHydration,
} from '@angular/platform-browser';
...
bootstrapApplication(RootCmp, {
providers: [provideClientHydration()]
});
According to the official Angular release note, this is just the beginning. They plan to explore partial hydration as the next step and work on several developer requests. You can find more about the Angular SSR development plan
- Experimental Jest Support Jest is one of the most-used testing frameworks among JavaScript developers. Angular has listened to developer requests and has introduced experimental Jest support with Angular 16.
All you have to do is install Jest using npm and update the angular.json file.
// Install jest
npm install jest --save-dev
// angular.json
{
"projects": {
"my-app": {
"architect": {
"test": {
"builder": "@angular-devkit/build-angular:jest",
"options": {
"tsConfig": "tsconfig.spec.json",
"polyfills": ["zone.js", "zone.js/testing"]
}
}
}
}
}
They plan to move all the existing Karma projects to Web Test Runner in future updates.
- esbuild-Based Build System Angular 16 introduces an esbuild-based build system for the development server (ng serve). Vite powers this new development server and uses esbuild to build artifacts.
This is still at the developer preview stage, but you can enable it by updating the angular.json file with the following.
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser-esbuild",
...
- Required Inputs In Angular 16, you can now define input values as required. You can either use the @Input decorator or the @Component decorator inputs array to define one.
export class App {
@Input({ required: true }) name: string = '';
}
// or
@Component({
...
inputs: [
{name: 'name', required: true}
]
})
- Router Inputs Angular 16 allows you to bind route parameters into component inputs, removing the need to inject ActivatedRoute into the components. To enable this feature, you must import RouterModule and enable the bindToComponentInputs property in the app.module.ts file.
@NgModule({
imports: [
...
RouterModule.forRoot([], {
bindToComponentInputs: true
})
...
],
...
})
export class AppModule {}
The following example shows how we can bind query params to component inputs.
// Route
const routes: Routes = [
{
path: "articles",
component: ArticleComponent,
},
];
// Component
@Component({})
export class ArticleComponent implements OnInit {
@Input() articleId?: string;
ngOnInit() {
}
}
Now, when you navigate to the articles route, you can pass query params using the name of the component input. In this case, an example URL will look like the following.
Full Original Article Is Here :https://softwaretechit.com/angular-16-is-launch-top-7-features/
Top comments (1)
Rymans Stationery: Ryman's Stationery is a go-to destination for all things office and school supplies, offering a diverse range of high-quality products. From sleek notebooks to ergonomic desk accessories, Ryman's caters to the needs of both students and professionals, combining functionality with style. Their commitment to excellence makes them a reliable choice for those seeking organizational solutions and creative tools.
Health Products: The health product market is witnessing a surge in innovation and awareness, with a focus on holistic well-being. From vitamins and supplements to fitness gadgets, the variety is vast. Consumers today are increasingly prioritizing self-care, and health product retailers play a crucial role in providing access to products that support physical and mental wellness. With the growing emphasis on preventive healthcare, health product brands like Ryman's contribute to a healthier and more informed society.