如何通过在Angular 8中更改html下拉列表来更改api url?
我正在尝试通过更改下拉列表来调用服务调用。这是我的密码 .html .服务如何通过在Angular 8中更改html下拉列表来更改api url?,angular,Angular,我正在尝试通过更改下拉列表来调用服务调用。这是我的密码 .html .服务 getUpdatenRecords(userId: number): any { return this.http.get(this.mainUrl + '/getcurrin/' + userId) } 我想在选择了this month时更改mainUrl,如localhost:8000/getcurrin/4/1 当选择上个月的时,如localhost:8000/getcurrin/4/0 任何帮助。
getUpdatenRecords(userId: number): any {
return this.http.get(this.mainUrl + '/getcurrin/' + userId)
}
我想在选择了this month
时更改mainUrl,如localhost:8000/getcurrin/4/1
当选择上个月的时,如localhost:8000/getcurrin/4/0
任何帮助。如果您想更改呼叫的baseUrl,您应该为此使用侦听器。通常您希望保持服务和组件逻辑相互独立。因此,如果服务将根据参数做出不同的反应,那么您应该将这些参数传递到服务调用中
服务
getUpdatenRecords(userId: number, monthSelection: number = 0): any {
return this.http.get(`${this.mainUrl}/${monthSelection}/${userId}`);
}
这样,服务就可以独立于组件逻辑。在这种情况下,monthSelection
如果未提供,则默认为0
然后修改组件事件处理程序,将属性传递给服务调用
组成部分
onMonthSelectionTabChanged(event) {
this.service.getUpdatenRecords(this.userId, event).subscribe();
}
同时确保取消订阅Ngondestory方法中可观察到的内容
希望这是有用的。祝你好运。要实现这一点,你需要在我在网络选项卡中记录时读取服务调用没有发生,并且在我更改下拉列表时数据也没有反映出来。我已经实现了相同的功能,但没有得到结果。你需要订阅从MothSelectionTabChanged
方法返回的observable。默认情况下,可观察对象为“冷”。这意味着如果你想触发事件,你需要订阅它。i、 e.this.service.getUpdateRecords(this.userId,event.subscribe()代码>完成后,您需要确保取消预订。
getUpdatenRecords(userId: number, monthSelection: number = 0): any {
return this.http.get(`${this.mainUrl}/${monthSelection}/${userId}`);
}
onMonthSelectionTabChanged(event) {
this.service.getUpdatenRecords(this.userId, event).subscribe();
}