Angular — New Control flow with a working example

angular-—-new-control-flow-with-a-working-example

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) {
  Inside if
} @else if (showAnotherIf) {
  Inside else if
} @else {
  Inside else
}

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 a @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'];
}

    @for (item of skills; track item; let i = $index, f = $first; let l = $last, ev = $even, o = $odd; let co = $count) {
  • {{item}}
    • Index - {{i}}
    • Is First - {{f}}
    • Is Last - {{l}}
    • Is even - {{ev}}
    • Is odd - {{o}}
    • Count - {{co}}
  • } @empty {
  • No item
  • }

Switch case

The new switch case control flow can be used as follows. You can use @default block to mention the 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) {
    Rendering case 1
  }
  @case (2) {
    Rendering case 2
  }
  @case (3) {
    Rendering case 3
  }
  @default {
    Rendering default
  }
}

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.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-choose-the-license-of-your-github-repository

How to choose the license of your GitHub repository

Next Post
hacktoberfest-2023-contributors-wanted:-additional-translations-for-the-user-statistician-github-action

Hacktoberfest 2023 Contributors Wanted: Additional Translations for the user-statistician GitHub Action

Related Posts