DEV Community

Cover image for What's new in Angular version 12?πŸ”₯
Yuvaraj
Yuvaraj

Posted on β€’ Originally published at yuvaraj.hashnode.dev

8 1

What's new in Angular version 12?πŸ”₯

Hello Everyone πŸ‘‹

Angular version 12 was officially released yesterday with some cool features. In this article we are going to see the two new features from Angular 12 release.

1. Sass support in Component Inline Style

2. Nullish Coalescing in Angular Template

SASS In Component Inline Style

Problem: In the previous Angular versions, Sass features was restricted to use only when it is used in an external .scss file. It was not possible to use Sass feature when used as an inline styles in the styles field of the @Component decorator.

To explain with the scenario, you can see from the below code, a new variable called $color-red is created & assigned it to h1 color property.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<h1>Hello world from {{title}}</h1>`
  styles: [
  `
  $color-red: red; // declaring a Sass variable
  h1 {
    color: $color-red; // using the Sass variable here
  }
  `
  ]
})
export class AppComponent {
  title = 'angular';
}

Enter fullscreen mode Exit fullscreen mode

Ideally, the h1 content should have shown in the red color. But, it is not. That's the problem till Angular version 11.

Here's the output from browser:

older-angular-sass.png

Solution: Starting in Angular version 12, Angular components will now support inline Sass in the styles field of the @Component decorator.

With the above same code, if you run the application in the Angular v12, it will show the h1 content in red color.

new-angular-11-sass.png

Note: If you are upgrading from Angular v11 to v12, set "inlineStyleLanguage": "scss” to angular.json file. If you are creating a new Angular v12, this will be available by default.

Nullish Coalescing in Angular Template

The nullish coalescing operator (??) was limited to use only in the typescript class files. In the previous version of angular, it was not possible to use this operator in the Angular template.

Starting from Angular version 12, it can be used in the html templates as well. πŸ‘

Previously, one has to use below syntax to know if the value is null or undefined.

{{amount !== null && amount !== undefined ? amount : 0 }}
Enter fullscreen mode Exit fullscreen mode

Now, it can be simplified as,

{{amount ?? 0 }}
Enter fullscreen mode Exit fullscreen mode

I hope it was useful. Thanks for reading it!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (2)

Collapse
 
captaindev profile image
Rami β€’

Thanks for sharing πŸ‘
+= Strict mode is the default starting version 12+

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay