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

197
Views
How one service (not singleton) can be used by one directive and one component?

I have created a directive that is using component as its template. Both of them use the same service included in providers:

@Component({
  selector: 'my-component',
  providers: [MyService],
  ..
}

@Directive({
  selector: '[myDirective]',
  providers: [MyService]
})

Now here is the problem. They use the same service, but while one is changing the server, the directive that has been register to its change didn't got the change at all:

this.myService.dataUpdated.subscribe
      (
        (data: someObject) => {
          // Do something...
        }
      )

Seems to me they are not using the same service at all.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't see a reason why singleton should not be used for your scenario. It seems to me that singleton should be used on your case, that's why you don't see the changes at all.

Try to provide your service in your NgModule like this

 @NgModule({
  ...
  providers: [MyService]
  ...

Hope this helps.

over 4 years ago · Santiago Trujillo 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!