Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to…

Follow publication

Angular — New Control Flow with a Working Example

Angular — New control flow by Madhu Sudhanan

If you are watching recent Angular works, you’d probably know the addition of new control flows with new syntax. The new control flow is planned for the Angular v17 release that would probably face the public in November 2023. More details on why @ is chosen can be found here.

In the Angular v17.0.0-next.6 release, the team has shipped the control flow changes which are not production-ready but we can play around with it.

Let's see what are the available control flows.

If else statement

In previous versions, NgIf is used to show DOM elements conditionally which is declarative but is not suitable for the new change detection strategy. The new @if block will resolve this problem and will work well with signal-based change detection.

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})

export class AppComponent {
show = true;
showAnotherIf = false;
}
@if (show) {
<span>Inside if</span>
} @else if (showAnotherIf) {
<span>Inside else if</span>
} @else {
<span>Inside else</span>
}

Aliasing is useful sometimes for using the evaluation result inside the block context. You can specify an alias using the as keyword as follows.

@if (canLoad(); as isLoaded) { // canLoad() returns boolean.
<span>Is Loaded - {{isLoaded}}</span>
}

For loop

The for loop allows you to render the given content based on the iterable object and it provides some useful context properties to work with. In addition, it provides @empty block which will be rendered when no item is present in the given array.

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})

export class AppComponent {
skills = ['javascript', 'html', 'css'];
}
<ul>
@for (item of skills; track item; let i = $index, f = $first; let l = $last, ev = $even, o = $odd; let co = $count) {
<li>{{item}}
<ul>
<li>Index - {{i}}</li>
<li>Is First - {{f}}</li>
<li>Is Last - {{l}}</li>
<li>Is even - {{ev}}</li>
<li>Is odd - {{o}}</li>
<li>Count - {{co}}</li>
</ul>
</li>
} @empty {
<li>No item</li>
}
</ul>

Switch case

The new @switch case control flow can be used as follows. You can use @default block to mention default content to be rendered when no case is matched.

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
caseNo = 4;
}
@switch (caseNo) {
@case (1) {
<span>Rendering case 1</span>
}
@case (2) {
<span>Rendering case 2</span>
}
@case (3) {
<span>Rendering case 3</span>
}
@default {
<span>Rendering default</span>
}
}

How to enable the new control flow in the Angular v17.0.0-next.6?

Even though the changes are available on the package, you cannot directly use this now and will end up with the below error.

error NG5002: Invalid ICU message. Missing ‘}’ or error NG5002: Unexpected character “EOF” (Do you have an unescaped “{“ in your template? Use “{{ ‘{‘ }}”) to escape it.)

To try the new control flows now you have to add the below config in the angularCompileroptions .

{
"angularCompilerOptions": {
....
"_enabledBlockTypes": [
"if", "switch", "for"
]
}
}

I got your mind's voice, yeah _enabledBlockTypes is an internal one that will be changed eventually. But for now, it will allow you to play around with the control flows.

You can find the full working sample here.

Feel free to let me know if you have any comments.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Madhu Sudhanan

Software developer and a blogger. Fond of Angular, React, Vue and Blazor frameworks. Follow me on Twitter — @maddydeep28. PortFolio — https://madhust.github.io/

Responses (3)

Write a response

Thank you for the examples. A good highlighting of @if / @for / @switch keywords would make this new syntax wonderful.

2

NgIf is used to show DOM elements conditionally which is declarative but is not suitable for

Can you elaborate on this?

1

One thing I fell over is that it doesn't seem to be possible to do @if( store.myState() as state) { ...
Aliasing deep properties is quite useful and we use it in templates all the time

1