Is there any benefit of using the new @if instead of the classic ngIf directive?
*ngIf version:
@Component({
standalone: true,
selector: 'scrollbars',
imports: [NgIf, ScrollbarX, ScrollbarY],
template: `
<scrollbar-x *ngIf="horizontalUsed()"/>
<scrollbar-y *ngIf="verticalUsed()"/>
`
})
@if version:
@Component({
standalone: true,
selector: 'scrollbars',
imports: [ScrollbarX, ScrollbarY],
template: `
@if (verticalUsed()) {
<scrollbar-y/>
}
@if (horizontalUsed()) {
<scrollbar-x/>
}
`
})
Besides the syntax difference, I noticed that I don't need to import NgIf or CommonModule to make @if work, which is nice!
I would appreciate if someone can explain if it works differently behind the scene
Functionnaly speaking yes, An @if block replaces the usage of the ngIf directive.
However, there are some technical advantages to use the new control flow blocks (@for/@if/@switch).
CommonModule when build standalone components.@if vs *nglf
Additional Tips: @for vs *ngFor
Migrate to the new control flow syntax
Run this command to upgrade your projects: ng generate @angular/core:control-flow
Need More visit angular official doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With