*ngIf is a structural directive that adds or removes elements from the DOM based on a condition. [ngStyle] and [ngClass] are attribute directives that change the appearance but do not add or remove elements. (click) is an event binding, not a directive.
<div [ngClass]="{ 'highlight': isActive }">Hello</div>What happens when
isActive is true?The [ngClass] attribute directive adds or removes CSS classes based on the expression. When isActive is true, the highlight class is added to the div. It does not remove or replace the element.
The correct syntax for structural directives uses an asterisk (*) before the directive name, like *ngIf. The other options use invalid syntax for structural directives.
<div *ngFor="let item of items">{{ item }}</div>
<div *ngFor="items">{{ item }}</div>Why does the second
div cause an error?The *ngFor directive requires a syntax like *ngFor="let item of items" to declare the loop variable. The second div misses the let item of part, causing a syntax error.
<button (click)="show = !show">Toggle</button>
<div *ngIf="show">Visible</div>
Initially,
show is false. What is the rendered output after clicking the button twice?Starting with show = false, the first click sets show = true, so the div appears. The second click sets show = false again, so the div disappears. After two clicks, no div is visible.