我正在努力理解...... 如何实现此类服务的请求取消。
我在读,我应该使用$ q.defer()
angular.module('App').service('TService', function ($resource, portal) {
return $resource(portal.getUrlServer() + 'api/T/:id', { id: '@Id' }, {
T_GET: {
method: 'GET',
params:{
id_turno: '@id_turno',
},
url: portal.getUrlServer() + 'api/T/T_GET/'
},
G_GET_Turno: {
method: 'GET',
params: {
id_tramite_relevado : '@Id_Tramite_Relevado'
},
url: portal.getUrlServer() + 'api/T/G_GET_Turno/'
},
});
我想要做的是当两次调用任何方法时,我只想让最后一次调用运行并取消其他请求。
答案 0 :(得分:0)
来自AngularJS docs:
如果某个操作的配置指定它是可取消的,则可以取消与实例或集合相关的请求(只要它是“非实例”调用的结果)。
// ...defining the `Hotel` resource...
var Hotel = $resource('/api/hotel/:id', {id: '@id'}, {
// Let's make the `query()` method cancellable
query: {method: 'get', isArray: true, cancellable: true}
});
// ...somewhere in the PlanVacationController...
...
this.onDestinationChanged = function onDestinationChanged(destination) {
// We don't care about any pending request for hotels
// in a different destination any more
this.availableHotels.$cancelRequest();
// Let's query for hotels in '<destination>'
// (calls: /api/hotel?location=<destination>)
this.availableHotels = Hotel.query({location: destination});
};