我已经有以下代码来更改myVariable
private dataSubscription: Subscription;
myVariable: number;
this.dataSubscription = this.mydata.onReply$.subscribe((data: any) => {
this.myVariable = data.id;
});
但是,只要myVariable
发生变化,我就想触发下面的API。如何检测到'myVariable'
已更改并触发API?
this.myService.getMyFunction(this.myVariable).subscribe((data) => {
this.tobeUpdated = data;
)};
我认为这很简单,但我是棱角分明的新手,我需要帮助。任何答案都将不胜感激。
答案 0 :(得分:0)
您可以在分配时检查myVariable的值。如果值已更改,则发出另一个将触发getMyFunction的事件
private dataSubscription: Subscription;
this.dataSubscription = this.mydata.onReply$.subscribe((data: any) => {
if (this.myVariable !== data.id) {
this.myVariable = data.id;
this.variableChanged.emit(true);
}
});
现在在这里
variableChanged.subscribe(data => {
this.myService.getMyFunction(this.myVariable).subscribe((data) => {
this.tobeUpdated = data;
});
}