What’s new in Angular 15?
Published: 2022-12-15 14:01:27

<img src="https://d1fbh1knoaoilz.cloudfront.net/2022/12/What-s-new-in-Angular-15---Peerlist---cover.png" alt="What&#x2019;s new in Angular 15?"><p>Angular is an open-source framework, also the popular one. It's the first choice for new developers as it offers a host of features that can help to scale a project.</p><p>The best part about Angular is that it comes with many updates and features from time to time. And the latest version of the release is Angular 15.</p><p>Angular 14 has improved and exciting features, but Angular 15 is all about the platform's proper stability. Let's dive deep to learn what's new in Angular 15.</p><h2 id="1-stable-standalone-components-api"><strong>1. Stable standalone components API</strong></h2><p>The standalone component was an essential feature of Angular 14, used to build the Angular applications without designing the Ngmodules. In this version, the components have achieved a set degree of stability through different amendments and performance observation.</p><p>Also, the Angular community has ensured that with the stability feature, different standalone components will work in sync with Angular elements and HTTP clients. Moreover, you can use the following standalone API to bootstrap your application in a single component.</p><pre><code class="language-javascript">import {bootstrapApplication} from '@angular/platform-browser';
import {ImageGridComponent} from'./image-grid';

@Component({  
    standalone: true,  
    selector: 'photo-gallery',  
    imports: [ImageGridComponent],  
    template: `    &#x2026; <image-grid [images]="imageList"></image-grid>  		`,
})

export class PhotoGalleryComponent {  
    // component logic
}

bootstrapApplication(PhotoGalleryComponent);</code></pre><p></p><h2 id="2-directive-composition-api"><strong>2. Directive composition API</strong></h2><p>The directive code can be reused to another level in this version. The feature enables the developers to enhance different host elements with directives and equip Angular with the code reusable strategy, but that is possible with the compiler function. Additionally, directive composition API works with only standalone directives. For instance:</p><pre><code class="language-javascript">@Component({  
    selector: 'mat-menu',  
    hostDirectives: [HasColor, {    
        directive: CdkMenu,    
        inputs: ['cdkMenuDisabled: disabled'],    
        outputs: ['cdkMenuClosed: closed']  
    }]})

class MatMenu {}</code></pre><p>In the above code snippet, you can improve the matmenu with two different directives - cdkmenu and hascolor. Matmenu reuses all the inputs, outputs, and logic.</p><h2 id="3-stable-image-directory"><strong>3. Stable image directory</strong></h2><p>Angular 15 has introduced a few new features for the image directory. The automatic srcset generation feature ensures that an appropriately sized image is requested by generating the srcset attribute.</p><p>However, it can drastically reduce the download times for the images. Apart from this, there is a feature called fill mode. It causes the image to fill the parent container, removing the need to declare the image width and height.</p><p>Besides, it is a handy tool if you cannot determine the image size or if you are looking to migrate to the CSS background. You can use the standalone ngoptimised image directive in the module:</p><pre><code class="language-javascript">import { NgOptimizedImage } from '@angular/common';

// Include it in the necessary NgModule
@NgModule({  
    imports: [NgOptimizedImage],
})
class AppModule {}

// ... or a standalone Component
@Component({  
    standalone: true  
    imports: [NgOptimizedImage],
})
class MyStandaloneComponent {}</code></pre><p>Within the component, you can replace the images SRC attribute with ngsrc and ensure you specify the priority attribute.</p><h2 id="4-router-default-imports"><strong>4. Router default imports</strong></h2><p>To reduce the load on the router, the router auto unwraps the default exports while lazy loading. For instance, look at the following lazy component:</p><pre><code class="language-javascript">@Component({  
    standalone: true,  
    template: '...'
})

export default class LazyComponent { ... }</code></pre><p><strong>Before the above change, you had to use:</strong></p><pre><code class="language-javascript">{  
    path: 'lazy',  
    loadComponent: () => import('./lazy-file')
        .then(m => 	m.LazyComponent),
}</code></pre><p></p><p>Now the router will look for the default export, and if it finds it, it will use it in the auto mode. Moreover, it simplifies the route declaration to:</p><pre><code class="language-javascript">{  
    path: 'lazy',  
    loadComponent: () => import('./lazy-file'),
}</code></pre><h2 id="5-functional-router-guards"><strong>5. Functional router guards</strong></h2><p>Take a look at the example where you will be able to define a guard:</p><pre><code class="language-javascript">@Injectable({ providedIn: 'root' })
export class MyGuardWithDependency implements CanActivate {  
    constructor(private loginService: LoginService) {}  
    
    canActivate() {    
        return this.loginService.isLoggedIn();  
    }
}

const route = {  
    path: 'somePath', 
    canActivate: [MyGuardWithDependency]
};
</code></pre><p>Here the login service will implement more logic and guard; even though it is simple, you have different boilerplate codes. With the new functional guard, you can reflect the code:</p><pre><code class="language-javascript">const route = {  
    path: 'admin',  
    canActivate: [() => inject(LoginService).isLoggedIn()]
};</code></pre><p>Functional guards are compostable, and you can create factory-like functions. You can accept different configs and return a guard to function.</p><h2 id="6-automatic-imports-in-language-service-and-cli-implements"><strong>6. Automatic imports in language service and CLI implements</strong></h2><p>In the version, the language service can auto-import different components that you will use in the template. Additionally, you can generate a new standalone component with ng g component --standalone.</p><p>Here you can simply put the output of ng new. You can reduce the config in the first step by removing the main three elements - test.ts, polyfills.ts, and environments. After which, you can specify Angular in angular.json.</p><pre><code class="language-javascript">"polyfills": [  
    "zone.js"
]</code></pre><p>To reduce config overhead, you can use a .browser list that can allow you to define a target.</p><h2 id="7-cdk-listbox"><strong>7. CDK Listbox</strong></h2><p>There is a component CDK that offers a set of different primitives for building different UI components. In the V15, they have introduced another primitive that can be customized as per the use case.</p><p>The Angular list box modules provide different directives, which help to create customized list-based interactions based on the WAI and ARI list box pattern.</p><p>Additionally, by using the @angular/cdk/listbox, you will get the expected behavior for an accessible experience, including layout support, keyboard interaction, and focus management. All the directives apply to their roles to host different elements.</p><h2 id="8-better-stack-traces"><strong>8. Better stack traces</strong></h2><p>You can get lots of insights from different surveys and use them accordingly. Debugging struggles for Angular developers are real. When partnered with Chrome dev tools, you can fix this:</p><pre><code class="language-javascript">ERROR Error: Uncaught (in promise): Error
Error
at app.component.ts:18:11
at Generator.next (<anonymous>)
at asyncGeneratorStep (asyncToGenerator.js:3:1)
at _next (asyncToGenerator.js:25:1)
at _ZoneDelegate.invoke (zone.js:372:26)
at Object.onInvoke (core.mjs:26378:33)
at _ZoneDelegate.invoke (zone.js:371:52)
at Zone.run (zone.js:134:43)
at zone.js:1275:36
at _ZoneDelegate.invokeTask (zone.js:406:31)
at resolvePromise (zone.js:1211:31)
at zone.js:1118:17
at zone.js:1134:33</code></pre><p>The above code suffers from two main issues. A single line corresponds to the code the developer has used. Also, a lot of things come from third-party dependency.</p><p>There is no information about what caused the interaction error. But by making some changes, the stack changes can be improved.</p><h2 id="to-sum-up"><strong>To sum up, </strong></h2><p>There are many other changes in Angular version 15 you can try. It also depends on what project you are working on. The new stable update will surely be a game-changer in the development process. So you must <a href="https://www.esparkinfo.com/hire-angular-developers.html">hire an angular developer</a> to leverage the technology and get in-depth knowledge about the version.</p></br><p style="color:#757CF9;">This article is published w/ <a target="_blank" href="https://scattr.io?ref=dev&source=What’s new in Angular 15?">Scattr  ↗️</a></p>