Skip to content

Commit ab7a349

Browse files
authored
Czy wiesz, że w Angular 17 została wprowadzona alternatywa dla *ngSwitch? (#265)
1 parent d0fb471 commit ab7a349

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: post
3+
title: Czy wiesz, że w Angular 17 została wprowadzona alternatywa dla *ngSwitch?
4+
description: ""
5+
date: 2025-03-10T08:00:00+01:00
6+
published: true
7+
didyouknow: true
8+
lang: pl
9+
author: dmejer
10+
image: /assets/img/posts/2025-03-10-czy-wiesz-ze-w-angular-17-zostala-wprowadzona-alternatywa-dla-ngswitch/thumbnail.webp
11+
tags:
12+
- angular
13+
---
14+
W Angular 17 pojawiło się *built-it control flow*, które zostało ustabilizowane w wersji 18. Są to zamienniki dyrektyw `*ngIf` (opisaliśmy to [tutaj]({% post_url pl/2024-11-04-czy-wiesz-ze-od-angular-17-mozna-uzywac-if-zamiast-ngif %})), `*ngFor` (opisane [tutaj]({% post_url pl/2024-12-16-czy-wiesz-ze-w-angular-17-zostala-wprowadzona-alternatywa-dla-ngfor %})), `*ngSwitch`.
15+
16+
`@switch` jest odpowiednikiem `*ngSwitch`. Oferuje tą samą funkcjonalność. Przykładowe wykorzystanie:
17+
```
18+
// *ngSwitch
19+
<ng-container [ngSwitch]="color">
20+
<p *ngSwitchCase="'blue'">Blue</p>
21+
<p *ngSwitchCase="'red'">Red</p>
22+
<p *ngSwitchDefault>Default case</p>
23+
</ng-container>
24+
25+
// @switch
26+
@switch(color) {
27+
@case ('blue') {
28+
Blue
29+
}
30+
@case ('red') {
31+
Red
32+
}
33+
@default {
34+
Default case
35+
}
36+
}
37+
```
38+
39+
`@switch` poprawia czytelność, oddzielając logikę od tagów html.
40+
41+
## Dokumentacja
42+
- [https://angular.dev/guide/templates/control-flow](https://angular.dev/guide/templates/control-flow)
43+
- [https://angular.dev/api/core/@switch](https://angular.dev/api/core/@switch)
10.5 KB
Loading

0 commit comments

Comments
 (0)