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

查看:141
本文介绍了中止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;
    });
});

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

我在下面这个解决方案<一个href=\"http://stackoverflow.com/questions/21915834/angular-http-setting-a-promise-on-the-timeout-config\">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.

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

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