Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
(Angular) Why would you want multiple instances of the same service?

I might be missing something, and people usually ask "How to" not "Why to" whenever I google it

But what's the point of actually creating multiple instances of the same service where each instance has the same functions, same variables?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To hold state.

Imagine you have an application, which has multiple tabs. Let's say they display car information, so each tab has a different car. You provide service in "root tab" component, so instance of that service will be available to all sub-components/services within that that tab, and not outside. This way you write service that stores data of one car, which reduces service complexity.

Small example

@Injectable()
export class CarService {
  color = '';
}
@Component({
  ...
  providers: [CarService]
})
export class TabComponent {
  constructor(private car: CarService) {}
  getColor() { return this.car.color }
}

This way you can have

TabComponent
  getColor() -> red
TabComponent
  getColor() -> blue
TabComponent
  getColor() -> black
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!