Skip to main content

What is the difference between a structural and an attribute directive?

A structural and an attribute directive in Angular differ in what exactly they control in the DOM:

  1. Structural directive
  • Changes the DOM structure: adds, removes or repeats elements.
  • Usually used with *, for example *ngIf, *ngFor, *ngSwitch.
  • Example:
html
<p *ngIf="isVisible">Shown only if isVisible = true</p>
  1. Attribute directive
  • Changes the appearance or behavior of an element, but does not change the DOM structure.
  • Applied as an element attribute, for example [ngClass], [ngStyle] or a custom directive like appHighlight.
  • Example:
html
<p [ngStyle]="{'color': textColor}">Text with a dynamic color</p>

Conclusion: structural directives control the presence and number of elements, attribute directives, the style and behavior of existing elements.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.