David R
David R

Reputation: 15667

Modifying component's variable value through service

I have a group component in my application named CustomerListComponent where in which I've a boolean variable named isEligibleForBenefits and which is defaulted to false while initializing.

Now I'm in a situation where I need to invoke a REST call in one of my service and on the success event of that call I need to make this isEligibleForBenefits variable to true.

The question here is, manipulating a group component's value from a service allowed?, If so how can that be achieved?

Can someone help?

Upvotes: 0

Views: 38

Answers (1)

Nour
Nour

Reputation: 5889

Okay i will answer you briefly:

In you service that will make an api call:

import {Subject} from 'rxjs/Subject';


export class yourService {
     private messageSubject:Subject<boolean> = new Subject();

     public getMessageSubject():Subject<boolean>{
            return this.messageSubject:Subject;
     }


    private callApi(){
         http.whatEver.map(() => { this.messageSubject.next(true);})
    }
}

In your component:

export class yourComponent{
   constructor(private service:yourService){
      this.service.getMessageSubject.subscribe((value:boolean) => {
            this.isEligibleForBenefits = true; // or = value;
      });
   }
}

Upvotes: 1

Related Questions