0

I need to call/subscribe to the external service observable method and take value from it. I try it, it component constructer but always return undefined. I examine put console.log inside the method the value is there. need some expert helpt to take this object ->string value in angular.

     this.x:string; 
        constructor(
        private readonly socService: ocService) { }
    
        
      ngOnInit(): void {
       
    
        this.ocService.getActiveoc().subscribe(oc => { this.x= oc.id });
        
        //TODO For Testing
        if(this.x){
          console.log("ID" + this.x)
          this.store.dispatch(selectPatientAction({ x: this.x}));
        }
      }

   onScanX() {
    this.store.dispatch(scanX({x: this.x }));
    this.isocSelected = true;
  }

2 Answers 2

1

Everything you want to do with that value should be done in the subscribe method. Dispatch your action in that method too.

I'm assuming you are using NgRedux here.

    constructor(
       private readonly socService: ocService
       private store: NgRedux<AppState>
     ) { }
    
        
      ngOnInit(): void {
        this.ocService.getActiveoc().subscribe(oc => { 
          this.x = oc.id;
          this.store.dispatch(selectPatientAction({ x: this.x}));
        });
      }

   onScanX() {
    this.store.dispatch(scanX({x: this.x }));
    this.isocSelected = true;
  }
Sign up to request clarification or add additional context in comments.

4 Comments

I am a very beginner of angular can you please, show me example. I need assing that id also for using another method ?
Can you post the method that you need the id for and how you are using it?
Skeleton , it thanks full even, you can please show me how to call event inside the dispatcher. I try to search a lot of articles but still couldn't resolve it.
Thank you som much.. it is working now. i will do more testing
1

I think you have a typo in this section?

this.ocService.getActiveoc().subscribe(soc => { this.x= oc.id });

I think it should be soc.id rather than oc.id?

1 Comment

Can i get the screenshot about the payload of oc?

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.