gpt4 book ai didi

javascript - 如何跟踪 Angular 2 服务调用的进度

转载 作者:行者123 更新时间:2023-11-30 00:06:14 25 4
gpt4 key购买 nike

有人告诉我,我们使用服务来与数据库或其他数据存储库对话。当查询正在执行时,我希望能够在 UI 上放置一个进度条。

您如何跟踪完成百分比?谢谢

最佳答案

您可以选择 refer this to do it sanely with angular2 HTTP模块。

否则同样可以用 jQuery 中的 XHR progress 事件完成:

import { Subject } from 'rxjs/Subject';
export class ProgressLoader {
percentage$: Subject<any>;

constructor() {
this.percentage$ = new Subject();
$.ajax({
type: 'GET',
url: 'http://google.com',
xhr: function() {
var xhr = new window.XMLHttpRequest(),

xhr.addEventListener("progress", (evt) => {
this.percentage$.next(parseInt(evt.loaded / evt.total * 100, false) + '%');
}, false);
return xhr;
}
});
}
}

关于javascript - 如何跟踪 Angular 2 服务调用的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com