When working with Angular, controlling DOM structure and styling dynamically is a common use case. A great way to achieve this is by using Angular’s built-in directives like ngIf to conditionally display elements and ngStyle to apply dynamic styles. You may often find yourself needing to use both together in a clean and efficient way.
In this blog post, we’ll walk through how to combine ngStyle with ngIf and else, and how to do so across different Angular versions, including the latest version of Angular.
ngIf is a structural directive that conditionally includes or excludes an element from the DOM based on a boolean expression. It supports an optional else clause that allows rendering an alternative template when the condition is false.
ngStyle is an attribute directive that allows you to conditionally apply inline CSS styles to an element based on an expression.
In earlier Angular versions (2 to 12), you would often combine ngIf and ngStyle by using them separately but together on the same element. You may also define an alternative template with ngIf’s else clause.
Example (Angular 2-12):
<div *ngIf="isActive; else inactiveBlock" [ngStyle]="{'color': isActive ? 'green' : 'red'}"> Active: The component is active! </div <ng-template #inactiveBlock> <div [ngStyle]="{'color': 'gray'}"> Inactive: The component is inactive! </div> </ng-template>
In the example above:
In Angular 13 and newer, there have been performance and style handling improvements, but the basic combination of ngIf and ngStyle remains quite similar to the previous versions.
However, newer Angular versions allow for a cleaner syntax and better code organization due to enhancements in Angular’s Ivy rendering engine, which is default since Angular 9, but further optimized in later versions. The use of directives and their combination has been made more efficient, so performance is improved when using conditionals like ngIf.
Example (Angular 13+):
Active State: Active! Inactive State: Inactive.
Changes:
Starting from Angular 16, you can use @if and @else for a more natural control flow, similar to if-else statements in JavaScript. This new syntax removes the need for explicit ng-template tags and improves readability.
Combining ngStyle with the new control flow syntax is straightforward. You can use ngStyle on elements inside the @if and @else blocks, just as you would with the traditional approach.
Example (Angular 13+):
@if (isActive) {The component is active!} @else {The component is inactive.}
In this example:
When combining ngIf, ngStyle, and else conditions in Angular, consider these best practices:
Keep your template clean by avoiding complex logic inside ngIf or ngStyle. If the logic is complicated, move it to the component’s TypeScript code for clarity.
Example:
typescript
get activeStyles() { return this.isActive ? { color: 'green' } : { color: 'red' }; }
In the template:
Active State
If multiple elements require similar styles, consider using a shared method or a class instead of repeating ngStyle logic.
If the goal is just to hide or show an element, consider using [hidden] or CSS classes instead of ngIf to improve performance, as ngIf removes elements from the DOM entirely.
Example:
Visible when active