@for
Old Syntax
Need to import NgFor from @angular/common.
<span *ngFor="let el of elements; index as i; first as isFirst">
{{ i }}: {{ el.desc }} (first={{isFirst}})
</span>
Available “special” variables:
- index & count
- odd & even
- first & last
Adding trackBy: trackElFn is optional and is only needed when you are running into performance issues.
trackElFn(index: number, el: T) {
return el.id;
}
New Syntax
The track is mandatory in the new syntax.
@for(el of elements; track el.id; let i = $index; let isFirst = $first) {
{{ i }}: {{ el.desc }} (first={{isFirst}})
}
@switch
Old Syntax
Need to import NgSwitch, NgSwitchCase and NgSwitchDefault from @angular/common.
<div [ngSwitch]="aNumber">
<span *ngSwitchCase="1">ONE</span>
<span *ngSwitchCase="2">TWO</span>
<span *ngSwitchDefault>OTHER</span>
</div>
New Syntax
@switch(randomNumber) {
@case(1) {ONE}
@case(2) {TWO}
@default {OTHER}
}
@let
Old Syntax
This was previously not possible out of the box but the same could be achieved with an ngIf:
<span *ngIf="randomObs$ | async as randomNr">
Nr = {{ randomNr }}
</span>
New Syntax
Don’t forget the ending ;!
@let randomNr = randomObs$ | async;
Nr = {{ randomNr }}