【发布时间】:2021-04-13 05:12:26
【问题描述】:
我正在开发一个 Angular 应用程序,它有一个带有点击事件的按钮。 但是该方法是使用订阅异步的。 我需要返回调用另一个类中的另一个方法。
我需要在 listItems.getData() 完成后调用 player.start(),问题是 getData() 是在 html 中调用,我需要捕获此方法的终止事件以触发 start() 方法。
播放器.html:
<button color="primary" (click)="listItems.getData()" ...
ListItems.ts:
export class ListItems {
...
getData() {
/**
** I call getData() in Html context, making it impossible to
** get the result inside a class in typescript
**/
...
return this.http.get(url).subscribe(res => {
this.data = res;
}
}
}
player.ts:
export class Player {
constructor(public listItems: ListItems) {
}
start() {
/**
** start method only can be called after listItems.data get data
**/
let alldata = this.listItems.data.replace('\n', '');
....
}
....
}
【问题讨论】:
标签: angular typescript asynchronous async-await