中止ngResource使用一个承诺对象对象、ngResource

2023-09-13 05:01:34 作者:落在身边的幸福

我最近了解到,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;
    });
});

我想避免的时候(我想通过调用取消previous发送多个请求 aborter.resolve()

我在下面这个解决方案Angular $ HTTP:对超时配置设置承诺能否请你指点我,为什么它不工作?

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

推荐答案

看起来它是一个开放的问题,与角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.

 
精彩推荐
图片推荐