使用承诺对象中止 ngResource [英] Aborting ngResource using a promise object

查看:24
本文介绍了使用承诺对象中止 ngResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解到可以通过以毫秒为单位指定超时或传递延迟对象来中止 ngResource 请求.

I've recently learned that ngResource request can be aborted either by specifying a timeout in ms or passing a deferred object.

第二个解决方案似乎对我不起作用,我不知道我做错了什么.我创建了一个小提琴来演示这个问题 http://jsfiddle.net/HB7LU/10977/

The second solution does not seem to work for me, and I have no idea what I'm doing wrong. I've created a fiddle to demonstrate the problem http://jsfiddle.net/HB7LU/10977/

var myApp = angular.module('myApp',['ngResource']);

myApp.factory('myResource', function($resource) {
    return {
      getResource: function (aborter) {
        var resource = $resource(
            'http://api.openweathermap.org/data/2.5/weather?q=London,uk', {}, {
          query: {
            isArray: false,
            timeout: aborter.promise
          }
        });

        return resource;
      }
    };
});

myApp.controller('MyCtrl', function($scope, $q, $log, $timeout, myResource) {
    var aborter = $q.defer();
    setTimeout(function() {
       $log.info('Aborting...');
       aborter.resolve();
    }, 10);
    myResource.getResource(aborter).query().$promise.then(function(data) {
        $scope.data = data;
    });
});

我想避免一次发送多个请求(我想通过调用 aborter.resolve() 取消前一个请求.

I want to avoid sending multiple request at the time (I want to cancel the previous by calling aborter.resolve().

我正在关注这个解决方案 Angular $http : setting a对超时"配置的承诺你能告诉我为什么它不起作用吗?

I was following this solution Angular $http : setting a promise on the 'timeout' config Could you please advice me why it does not work?

推荐答案

看起来它是 Angular 1.3 的一个开放问题:github.com/angular/angular.js/issues/9332 如果你回退到 1.2.28,你的 jsfiddle 就可以工作了.

It looks like it's an open issue with Angular 1.3: github.com/angular/angular.js/issues/9332 You're jsfiddle works if you drop back to 1.2.28.

这篇关于使用承诺对象中止 ngResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆