PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Angular · Theory

Services

← All stacks

Theory

13/43

Services

A service is a class with @Injectable that holds shared work: fetch marks, cache them, compute totals. Components stay thin. Two pages inject the same MarksService and see the same data if providedIn: 'root'.

Board: MarksPage and ReportPage both call marksService.list(). Don’t new MarksService() inside a component — you get two copies and a bug that looks like ‘state reset’.

Trap: putting HTTP + alert() + routing inside one service method. Split: data in the service, navigation in the component.

Diagram
  MarksPage ──inject──► MarksService
  ReportPage ──inject──► MarksService
  one root instance
Exam tip

Why DI, not new. One MarksService example.

Example

import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  template: `
    <h1>{{ title }}</h1>
    <button (click)="toggle()">Toggle</button>
    <p *ngIf="open">Panel open</p>
  `,
})
export class AppComponent {
  title = "Angular practice";
  open = true;
  toggle() {
    this.open = !this.open;
  }
}

Services: toggle open. Say whether the panel is on screen after one click.

Short notes

  • Def@Injectable shared logic. Components stay thin.
  • RuleprovidedIn: 'root' for one app-wide instance.
  • Trapnew Service() inside a component.

Questions

1

What is an Angular service?

2

Why not new MarksService()?

Previous← ListsNextDependency Injection →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.