Run page function from service

Hi all ! Could anyone suggest please, if exist way to run function inside page from service ?

For example I have page.ts with function one and I have myService.ts. Can I run function one from myService.ts ?

I think you better create another service/provider with the “function one” and call it inside your myService.ts or page.ts.
But if you wanna try what you described, you can import the component inside your service… but I never tried to be honest.

1 Like

It’s fun challenge

try this

first you need to create an object with type Behavior Subject in the service , like this

private _CHANGES = new BehaviorSubject<boolean>(false)

then make a function to give access to that object , like this

get changes() {
  return this._CHANGES.asObservable();
}

now you are ready to go

inside the page.ts update your constructor to this

constructor(
  private myService: MyService
) {
  this.myService.changes.subscribe(
     changesSubmited => {
         if (changesSubmited) {
              this.functionHaveToRun();
          }
       }
  );
}

then , any time you need to run that function from your service , all you need to do is to do something like this

this._CHANGES.next(true);
1 Like

arazinejad, good day ! It is fantastic ! It is what I am looking for ! Thanks a lot.

1 Like