← Back to Angular, page 6

angular-0115 · Easy fresher

What is the difference between removing an element with @if and hiding it with the hidden attribute or CSS, and when do you choose each?

@if creates and destroys the element and its component, so its state and subscriptions are reset, while hidden or display none keeps the element alive and only hides it. Use @if when the content is expensive or should reset, and hidden when you toggle often and want to keep state.

Full answer

Both make something disappear from the screen, but they do different work.

@if (show) { <app-chart /> }: when show is false Angular removes the element and destroys any component inside it. Its state, form values, timers and subscriptions are gone. When show becomes true again Angular creates a new instance, and ngOnInit runs again.

[hidden]="!show" or [style.display]="show ? 'block' : 'none'" or a class: the element stays in the DOM and in memory. The component keeps its state and keeps running (timers, subscriptions, change detection). Only the browser hides it.

Choosing:

  • Use @if when the content is heavy, when it should start fresh each time, or when it must not run in the background.
  • Use hidden or CSS when you toggle often (tabs that keep their form data), when creation is expensive and you want to keep the result, or when you animate with CSS transitions.

Other points:

  • The hidden attribute is overridden by CSS such as display: flex. If a style sets display, hidden may not hide the element. Check with a class that sets display: none.
  • display: none removes the element from the accessibility tree and from the tab order. visibility: hidden hides the element but keeps its space, and opacity: 0 keeps it focusable, which is bad for keyboard users.
  • With @defer, you can delay creation until a condition or visibility, which helps heavy content.
  • For tabs with forms, hiding keeps typed values, but removing loses them unless the state lives in a parent or service.
  • Hidden components still take part in change detection and keep their subscriptions running.
typescript
import { Component, OnDestroy, signal } from '@angular/core';

export const events: string[] = [];

@Component({ selector: 'app-note', template: `<input aria-label="Note" />` })
export class NoteComponent implements OnDestroy {
  constructor() { events.push('created'); }
  ngOnDestroy(): void { events.push('destroyed'); }
}

@Component({
  selector: 'app-toggle-demo',
  imports: [NoteComponent],
  template: `
    <button (click)="show.set(!show())">Toggle</button>
    @if (show()) { <app-note /> }
    <app-note [hidden]="!show()" />
  `,
})
export class ToggleDemoComponent {
  readonly show = signal(true);
}

Angular 20.3, strict template check passes. A Karma test shows that the child under @if is destroyed and recreated, while the hidden one is created once.

Key points

  • `@if` creates and destroys, so state and subscriptions reset.
  • `hidden` or CSS display keeps the element alive and only hides it.
  • Choose by cost, state needs, and how often you toggle.
  • Beware CSS overriding `hidden`, and focusable items hidden only with `opacity`.

Common mistakes

  • Using `hidden` for heavy components that keep running in the background.
  • Using `@if` for tab panels and losing form input.
  • Hiding with `opacity: 0` and leaving the content focusable.

Follow-ups

  • How would you keep tab form state while using `@if`?
  • What is the effect on accessibility of `display: none`?
  • How does `@defer` fit in?

Tips

  • State the lifecycle difference first.
  • Give tabs as the example for `hidden`, and heavy charts for `@if`.
  • Mention focus and accessibility.

Evaluation rubric

  • Explains creation, destruction and state.
  • Gives choice criteria.
  • Mentions CSS and accessibility pitfalls.