由于angular的HTTP请求返回promise类型的数据,通过subscribe()方法来接收返回数据,因此可以通过调用取消订阅 unsubscribe()方法来实现中断HTTP请求。
实现方法用例:
subscribes:any;
getChannels() {
this.subscribes = this._service.getChannels(connectionName).subscribe((channels: any) => {
......
})
}
stopGet() {
this.subscribes.unsubscribe();
}
如果保存subscribe的变量subscribes不能确定,可以用json类型的变量来存储,通过key来获得某个HTTP请求的subscribe数据,再调用unsubscribe方法即可。
实现方法用例:
subscribes = {};
getChannels() {
this.subscribes['someKey'] = this._service.getChannels(connectionName).subscribe((channels: any) => {
......
})
}
stopGet() {
this.subscribes['someKey'] .unsubscribe();
}
angularjs可以通过$q来实现http请求中断。