AngularJS - 使用ngMockE2E $ httpBackend我怎么能延迟特定的响应? [英] AngularJS - Using ngMockE2E $httpBackend how can I delay a specific response?
问题描述
我想推迟以下whenGET响应:
I'd like to delay the response to the following whenGET:
$httpBackend.whenGET(/^foobar/).respond(function () {
return [200,{}];
});
不过这似乎是不可能使用$超时同步做到这一点,所以我不知道如何处理呢?
However it seems impossible using $timeout to do this synchronously, so I'm not sure how to approach this?
推荐答案
如果你想拖延只是一个具体的回应,可能比你耽误范围财产效应初探转让。
If you want to delay only a specific response, than you may delay assignment of the reponse to scope property.
如果你换你的电话到您的自定义服务方法,可能比你的包裹到响应的承诺,并在需要的时候解决这个问题:
If you wrap your call into your custom service method, than you may wrap the response into the promise and resolve it when needed:
JS:
angular.module('app').service('myService', function($q, $timeout){
this.getData = function() {
var delay = 300,
origPromise = $http.get('someUrl'),
deferred = $q.defer();
$timeout(function() {
deferred.resolve(origPromise);
}, delay);
return defered.promise;
};
});
:
要延迟设置为所有的请求,你可以申请这个解决方案的响应拦截
To set the delay to all requests, you may apply this solution to the response interceptor
这篇关于AngularJS - 使用ngMockE2E $ httpBackend我怎么能延迟特定的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!