2016-07-17 54 views

回答

1

您可以或者refer this to do it sanely with angular2 HTTP模块。

否则同样可以用XHR progress事件jQuery中进行:

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; 
     } 
    }); 
    } 
} 
+0

确定。所以我的问题是,那么你将不得不使用可观察的来管理组件中的百分比? – arcee123

+0

@ arcee123您可以使用EventEmitter发出新的百分比值,并在视图中使用的组件中订阅它(更新后的代码)。 – codef0rmer