Cot*_*ten 34 angularjs angularjs-service
我有一些角度工厂,用于对传统的ASP.NET .asmx Web服务进行ajax调用,如下所示:
module.factory('productService', ["$http",
function ($http) {
return {
getSpecialProducts: function (data) {
return $http.post('/ajax/Products.asmx/GetSpecialProducs', data);
}
}
} ]);
Run Code Online (Sandbox Code Playgroud)
我正在本地网络上进行测试,因此响应时间"太"了.是否有一种聪明的方法可以将$ http延迟几秒钟来调用模拟连接错误?
或者我是否需要在$ timeout中包含对工厂方法的所有调用?
$timeout(function() {
productService.getSpecialProducs(data).success(success).error(error);
}, $scope.MOCK_ajaxDelay);
Run Code Online (Sandbox Code Playgroud)
Ste*_*ers 53
有趣的问题!
正如您自己提到的,$timeout延迟呼叫是最合理的选择.$timeout你可以$http 在一个$timeout承诺中推送一个响应拦截器,而不是在任何地方进行调用,如文档中$http概念性概述的那样,并在一个配置块中注册它.这意味着所有$http呼叫都会受到$timeout延迟的影响.有点像:
$httpProvider.interceptors.push(function($timeout) {
return {
"response": function (response) {
return $timeout(function() {
return response;
}, 2500);
}
};
});
Run Code Online (Sandbox Code Playgroud)
作为"模拟连接不良?"的奖励,您可以随意拒绝或绝对做任何事情.嘿嘿嘿.
Vin*_*243 16
新的chrome设备模拟器具有网络限制功能:

要到达目的地:在Google Chrome中,按F12打开开发人员工具.然后,在左上角,单击"切换设备模式"图标(左侧的"元素"菜单).
| 归档时间: |
|
| 查看次数: |
24890 次 |
| 最近记录: |