Bird
Raised Fist0
Angularframework~5 mins

Container and presentational components in Angular

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Container and presentational components help organize your app by separating logic from display. This makes your code easier to understand and maintain.

When you want to keep UI simple and focused on showing data.
When you want to reuse UI parts without repeating logic.
When you want to separate data fetching and state management from how things look.
When working in a team where designers and developers work on different parts.
When you want to test UI and logic separately.
Syntax
Angular
/* Presentational component */
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-user-card',
  standalone: true,
  template: `
    <div>
      <h2>{{ user.name }}</h2>
      <p>{{ user.email }}</p>
    </div>
  `
})
export class UserCardComponent {
  @Input() user!: { name: string; email: string };
}

/* Container component */
import { Component } from '@angular/core';
import { UserCardComponent } from './user-card.component';

@Component({
  selector: 'app-user-container',
  standalone: true,
  imports: [UserCardComponent],
  template: `
    <app-user-card [user]="user"></app-user-card>
  `
})
export class UserContainerComponent {
  user = { name: 'Alice', email: 'alice@example.com' };
}

Presentational components focus on how things look and receive data via @Input().

Container components handle data and logic, then pass data down to presentational components.

Examples
This presentational component emits an event when clicked, letting the container handle what happens next.
Angular
import { Component, Output, EventEmitter } from '@angular/core';

/* Presentational component with event output */
@Component({
  selector: 'app-button',
  standalone: true,
  template: `<button (click)="clicked.emit()">Click me</button>`
})
export class ButtonComponent {
  @Output() clicked = new EventEmitter<void>();
}
The container listens to the event and runs logic, keeping the button simple.
Angular
import { Component } from '@angular/core';
import { ButtonComponent } from './button.component';

/* Container listens to presentational event */
@Component({
  selector: 'app-button-container',
  standalone: true,
  imports: [ButtonComponent],
  template: `<app-button (clicked)="onClick()"></app-button>`
})
export class ButtonContainerComponent {
  onClick() {
    alert('Button clicked!');
  }
}
Sample Program

This example shows a presentational component MessageComponent that only displays a message and a clear button. The container MessageContainerComponent manages the message state and passes it down. It also handles showing and clearing the message.

Accessibility is included with role="alert" and aria-live="polite" so screen readers announce message changes.

Angular
import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-message',
  standalone: true,
  template: `
    <div role="alert" aria-live="polite">
      <p>{{ message }}</p>
      <button (click)="clear.emit()" aria-label="Clear message">Clear</button>
    </div>
  `
})
export class MessageComponent {
  @Input() message = '';
  @Output() clear = new EventEmitter<void>();
}

@Component({
  selector: 'app-message-container',
  standalone: true,
  imports: [MessageComponent],
  template: `
    <app-message [message]="currentMessage" (clear)="clearMessage()"></app-message>
    <button (click)="showMessage()">Show Message</button>
  `
})
export class MessageContainerComponent {
  currentMessage = '';

  showMessage() {
    this.currentMessage = 'Hello from container!';
  }

  clearMessage() {
    this.currentMessage = '';
  }
}
OutputSuccess
Important Notes

Use @Input() to pass data from container to presentational components.

Use @Output() with EventEmitter to send events from presentational to container.

Keep presentational components simple and reusable by avoiding data fetching or complex logic inside them.

Summary

Container components handle data and logic.

Presentational components focus on displaying data and UI.

This separation makes your app easier to maintain and test.

Practice

(1/5)
1. In Angular, what is the main role of a container component?
easy
A. To manage CSS and animations
B. To display UI elements and styles
C. To define routes and navigation
D. To handle data fetching and business logic

Solution

  1. Step 1: Understand container component responsibility

    Container components are designed to manage data and logic, such as fetching data or handling user actions.
  2. Step 2: Differentiate from presentational components

    Presentational components focus on showing data and UI, not on data handling.
  3. Final Answer:

    To handle data fetching and business logic -> Option D
  4. Quick Check:

    Container = data and logic [OK]
Hint: Container = data and logic, Presentational = UI only [OK]
Common Mistakes:
  • Confusing container with presentational component roles
  • Thinking container manages styles or routes
  • Assuming container handles only UI display
2. Which of the following is the correct way to pass data from a container to a presentational component in Angular?
easy
A. Use ngModel in container component only
B. @Input() data: any; in presentational, bind in container template
C. @Output() data: any; in presentational, bind in container template
D. Directly modify presentational component's variables from container

Solution

  1. Step 1: Identify Angular data flow syntax

    Data flows from container to presentational via @Input() properties.
  2. Step 2: Understand binding in container template

    The container passes data by binding to the presentational component's input property in its template.
  3. Final Answer:

    @Input() data: any; in presentational, bind in container template -> Option B
  4. Quick Check:

    Data down via @Input() [OK]
Hint: Use @Input() to pass data down from container [OK]
Common Mistakes:
  • Using @Output() to pass data down instead of events up
  • Trying to modify child variables directly
  • Confusing ngModel with input binding
3. Given this Angular container component template:
<app-user-list [users]="userArray" (selectUser)="onUserSelect($event)"></app-user-list>

What is the role of (selectUser) here?
medium
A. It defines a CSS class for styling
B. It binds data from container to presentational component
C. It listens to an event emitted by the presentational component
D. It initializes the component's state

Solution

  1. Step 1: Recognize Angular event binding syntax

    Parentheses around selectUser indicate event binding from child to parent.
  2. Step 2: Understand event emission from presentational component

    The presentational component emits selectUser event, container listens and runs onUserSelect.
  3. Final Answer:

    It listens to an event emitted by the presentational component -> Option C
  4. Quick Check:

    Parent listens to child event with (event) [OK]
Hint: Parent uses (event) to listen to child events [OK]
Common Mistakes:
  • Thinking (selectUser) passes data down
  • Confusing event binding with property binding
  • Assuming it styles or initializes state
4. What is wrong with this presentational component code snippet?
@Component({
  selector: 'app-item',
  template: `<div>{{item.name}}</div>`
})
export class ItemComponent {
  item: any;
}
medium
A. Missing @Input() decorator on item property
B. Template syntax is incorrect
C. Selector name is invalid
D. Component class should be abstract

Solution

  1. Step 1: Check data input declaration

    The presentational component expects data from parent, so item must be decorated with @Input() to receive it.
  2. Step 2: Verify template and selector

    Template syntax and selector are valid; no abstract class needed.
  3. Final Answer:

    Missing @Input() decorator on item property -> Option A
  4. Quick Check:

    @Input() needed to receive data [OK]
Hint: Add @Input() to receive data in presentational component [OK]
Common Mistakes:
  • Forgetting @Input() on input properties
  • Thinking template interpolation is wrong
  • Assuming selector must be different
5. You want to create a container component that fetches a list of products and passes it to a presentational component for display. Which approach best follows Angular container and presentational component patterns?
hard
A. Container fetches products, stores in a variable, passes via @Input(); presentational only displays list
B. Presentational component fetches products and emits events to container
C. Container and presentational both fetch products independently
D. Presentational component manages fetching and state internally

Solution

  1. Step 1: Identify container responsibility

    The container should handle fetching data and managing state.
  2. Step 2: Identify presentational responsibility

    The presentational component should only display data received via @Input() without fetching or managing state.
  3. Final Answer:

    Container fetches products, stores in a variable, passes via @Input(); presentational only displays list -> Option A
  4. Quick Check:

    Container = data fetch, Presentational = display [OK]
Hint: Container fetches data, presentational displays it [OK]
Common Mistakes:
  • Letting presentational fetch data
  • Duplicating data fetch in both components
  • Mixing data logic inside presentational