Call a method from inside of subscribe

410 Views Asked by At

Im trying to call a method from inside of subscribe in angular/ionic. But its not working. Here is my code:

somemethod()
{
  const browser = this.iab.create('https://someurl.com');
  browser.on('loadstart').subscribe(event=>{
    if(event.url === 'https://specificurl.com'){
      browser.close();
 // trying to call a member method from here , working
this.mymethod();
    }
  });
  browser.on('exit').subscribe(eventx=>{
   // trying to call a member method from here 
this.mymethod();
});
}
mymethod()
{
http.get<any>(url).subscribe(e=> { 
this.myvar = e.response; // this data is shown to user, but the user interface is not changing until something is clicked by the user
});
}
1

There are 1 best solutions below

8
Joosep Parts On

Just in case... You create the in app browser for url https://someurl.com but you subscribe and check for https://specificurl.com. What if you change it to listen for what you create?

if(event.url == 'https://someurl.com'){
      console.log(event)
      browser.close();
    }

If the block itself is reachable go over the scope. Example:

@Component({
  templateUrl: 'app.html',
})
export class MyApp {
  rootPage: any = TabsPage;
  response$: Observable<any>;

  constructor(platform: Platform) {
    platform.ready().then(() => {
      this.myMethod();
    });
  }
  myMethod() {
    console.log('cool');
    // TODO supply valid url and other params if neccessary
    this.response$ = http.get<any>(url).pipe(data => {return data;})
  }
}

Then use response$ like so:

<div>{{ response$ | async}}</div>

https://stackblitz.com/edit/ionic-p5qfps?file=app%2Fapp.component.ts