← Back to Angular, page 8

angular-0154 · Easy fresher

What belongs in a component and what belongs in a service? Give examples from a school app.

A component owns the screen: its template, the user's input and view-only state. A service owns logic and data that is not about one screen: HTTP calls, shared state, business rules and helpers. Keeping them apart makes code reusable, testable and easier to change.

Full answer

A component answers the question "what does this part of the screen look like and what does the user do here?" A service answers "how does the app get, keep or calculate this data?"

Put in the component:

  • The template and styles.
  • Reading input from the user (clicks, typing) and calling a method for it.
  • State that only this screen needs, such as "is the menu open" or "which tab is active".
  • Simple formatting for display.

Put in a service:

  • HTTP calls: getStudents(), saveMarks().
  • Data that several components share, such as the logged in user or the selected class. A service with signals works as a small store.
  • Business rules, such as how to calculate a grade or whether a student can enrol. These do not depend on any screen.
  • Wrappers for browser APIs (localStorage, window), so tests and server rendering can replace them.

Why split:

  1. Reuse. The marks page and the report page can both use MarksService.
  2. Testing. A calculation in a service is a plain class test. A calculation inside a component needs a rendered component.
  3. Lifetime. A component is created and destroyed as the user moves around. A service in providedIn: 'root' lives as long as the app, so data survives navigation.
  4. Readability. A component with 600 lines mixes HTTP, rules and markup. Small components are easier to read.

Do not go too far in the other direction: a service for every tiny helper, or state placed in a service when only one component uses it, adds files and no value. A good rule is to start in the component, and move code to a service when a second place needs it or when it has no relation to the view.

A service is created through dependency injection: inject(MarksService). The component never uses new MarksService().

typescript
import { Component, Injectable, inject, signal } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class GradeService {
  readonly selectedClass = signal('10A'); // shared state

  grade(marks: number): string {
    if (marks >= 75) return 'A';
    if (marks >= 50) return 'B';
    return marks >= 35 ? 'C' : 'Fail';
  }
}

@Component({
  selector: 'app-marks-row',
  template: `<p>{{ name }}: {{ marks }} ({{ grades.grade(marks) }}) in class {{ grades.selectedClass() }}</p>`,
})
export class MarksRowComponent {
  protected readonly grades = inject(GradeService);
  name = 'Asha';
  marks = 82;
}

@Component({
  selector: 'app-class-picker',
  template: `<button type="button" (click)="grades.selectedClass.set('10B')">Switch to 10B</button>`,
})
export class ClassPickerComponent {
  protected readonly grades = inject(GradeService);
}

Angular 20.3, strict template check passes. A Karma test injects the service in the component and checks the grade text and that two components see the same shared value.

Key points

  • Components own the view and user interaction. Services own data access, shared state and business rules.
  • A root service lives as long as the app, so its data survives navigation.
  • Splitting gives reuse, easier tests and smaller components.
  • Start in the component and extract when a second use appears.

Common mistakes

  • Making HTTP calls and grade rules inside the component.
  • Creating a service with `new` instead of injecting it.
  • Moving every small thing into a service without need.

Follow-ups

  • Where would you keep the logged in user?
  • How does a service share state between two sibling components?
  • Why is a calculation easier to test in a service?

Tips

  • Use two concrete school examples.
  • Mention lifetime and testing as the reasons.
  • Say you extract when needed and do not over-split.

Evaluation rubric

  • Gives a clear rule for the split.
  • Lists reasons: reuse, testing, lifetime.
  • Warns against over-engineering.