简体   繁体   English

从订阅内部调用方法

[英]Call a method from inside of subscribe

Im trying to call a method from inside of subscribe in angular/ionic.我试图从 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
});
}

Just in case... You create the in app browser for url https://someurl.com but you subscribe and check for https://specificurl.com .以防万一...您为 url https://someurl.com创建了应用内浏览器,但您订阅并检查了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:如果块本身可以通过 scope 到达 go。示例:

@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:然后像这样使用response$

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM