0

For reasons beyond the scope of this question, I have had to stop using @injectable. I converted my services that use it, just manually newing them up rather than using @injectable and it works for all services except for my service that uses subscribe My service that uses subscribe did work before I had to remove @injectable. Can I get it to work without @injectable?.

Code:

Service:

import { Subject } from 'rxjs/Subject';

export class ResultSelectionService {
  // Observable string sources
  private resultSelectedSource = new Subject();
  // Observable string streams
  resultSelected$ = this.resultSelectedSource.asObservable();
  // Service message commands
  selectResult(result: any, place: any) {
    this.resultSelectedSource.next({result: result, place: place});
  }
}

service user (the selectResult function does execute - Good):

//arrow function because when called above "this" is an html element
resultClick = (event) => {
    this.resultSelectionService.selectResult(event.data.result, event.data.place);
}

method that should execute but does not ( code never gets inside result => {:

constructor( ) {
this.resultSelectionService = new ResultSelectionService();
    this.resultSelectionService.resultSelected$.subscribe(
        result => {

            this.selectedResult = { result };

            $('#result-details-modal').appendTo("body").modal('show');

            $('#result-details-modal').on('shown.bs.modal', (e) => {
                this.resizeMap();
                this.canGetAddress = true;
                this.addMarkersToMap(this.map);
                //this.bindAutoCompleteToMap();
            })
            console.log(this.selectedResult.result);
        });
}

Can I get the subscribed function to execute without using @injectable? If not, how can I work around this, creating something similar?

4
  • 2
    These to things are entirely unrelated. You should rather try to get your setup fixed. @Injectable() is not known to cause any issues. Commented Oct 7, 2016 at 7:34
  • The relationship between the caller of selectResult and the caller of resultSelected$.subscribe is not made at all clear in your question. Are they in the same component? Your statement that you've had to stop using @Injectable is concerning and suggests the problem is elsewhere. Commented Oct 7, 2016 at 7:38
  • @GünterZöchbauer What do you suggest I do to get my setup fixed? I have tried your only suggestion Commented Oct 7, 2016 at 7:38
  • @GünterZöchbauer Since I managed to get this working I have discovered that some of my other services in my app that I did not stop using @injectable for actually do work, meaning that nothing is wrong with my setup or with @injectable. Something is just wrong with my providers. I have updated my other question. Thanks. Commented Oct 7, 2016 at 8:13

1 Answer 1

3

What you need here is a single instance of ResultSelectionService, that's why your subscribe method is never called.

You need to add @Injectable() on ResultSelectionService, add it in your forRoot() module method (or directly in AppModule's providers if it's the only module you have) and use it like that:

constructor( private resultSelectionService:ResultSelectionService ) {
    this.resultSelectionService.resultSelected$.subscribe(
        result => {

            this.selectedResult = { result };

            $('#result-details-modal').appendTo("body").modal('show');

            $('#result-details-modal').on('shown.bs.modal', (e) => {
                this.resizeMap();
                this.canGetAddress = true;
                this.addMarkersToMap(this.map);
                //this.bindAutoCompleteToMap();
            })
            console.log(this.selectedResult.result);
        });
}

This way, you'll be sure that only one instance of your ResultSelectionService is provided everywhere in your app, and you'll be able to trigger the observable from anywhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.