Angular2:取消循环中的Http请求

ind*_*257 3 observable rxjs angular2-http angular2-observables angular

我有一系列通过循环遍历小工具的Http调用.有没有办法中止所有请求

      for (let gadget of gadgets) {
               this.userService.getGadgetsData(gadget.Id, gadget.Name).subscribe(gadgetsData => {   
    });
}
Run Code Online (Sandbox Code Playgroud)

我在component.service.ts中的服务代码

@Injectable()
export class UserService {
    constructor(public _http: Http) { }
 getGadgetsData() {

        return this._http.get(this._dashboards, { headers: this.getHeaders() })
            .map((res: Response) => res.json());
    }

}
Run Code Online (Sandbox Code Playgroud)

wan*_*eam 5

如果这是你想要的,请看看.

observableList: [];

......

for (let gadget of gadgets) {
    let obs = this.userService.getGadgetsData(gadget.Id, gadget.Name).subscribe(gadgetsData => {
    });
    this.observableList.push(obs);
}

...
onClick() {
    for (let i = 0; i < this.observableList.length; i++) {
        this.observableList[i].unsubscribe();
    }
}
Run Code Online (Sandbox Code Playgroud)