NG.II · Angular 22 practice question
Consider an Angular component where you want to dynamically control CSS classes and inline styles based on the current state managed by Signals. Which of the following approaches correctly uses signal-driven bindings to achieve both dynamic class toggling and inline style setting?
import { signal, computed } from '@angular/core';
@Component({...})
export class ExampleComponent {
isActive = signal(false);
size = signal(100);
// Assume we want to apply 'active' class and a 'width' style
// when isActive is true, and the style dynamically reflects size.
}
Check answer and explanation
Correct answer: <div [class.active]="isActive()" [style.width.px]="size()">...</div>
Explanation
Angular supports property binding for classes using the shorthand `[class.className]="booleanExpression"` or `[class.className]="signalCall()"` when the expression evaluates to a boolean. For inline styles, binding an object literal like `[style]="{ key: value }"` is the standard approach, but when dealing with primitive values or direct signal results for specific properties (like width), the `[style.property.unit]="value"` syntax is available. Option A correctly uses the attribute binding syntax for both conditional class toggling and unit-based style binding directly from signals. Option B is valid but less concise than A for this specific use case. Option C and D use incorrect syntax for dynamically setting style values based on the signal output.
Common trap
Developers might confuse the syntax for class binding (`[class.name]="true"`) with general property binding, or struggle with how unit-based styles are bound using the signal value.
Source: Templates - Angular Developer Guide, https://angular.dev/guide/templates
Checked against the cited text on 2026-11-27.