Delay an angular.js $http service

前端 未结 8 1124
感动是毒
感动是毒 2021-01-31 16:32

I have some angular factories for making ajax calls towards legacy ASP.NET .asmx web services like so:

module.factory(\'productService\', [\"$http\",
function ($         


        
8条回答
  •  后悔当初
    2021-01-31 16:43

    You could use the $q service for defer().promise pattern:

    function someFunction(MOCK_ajaxDelay) {
       var deferred = $q.defer();
       $http.post('/ajax/Products.asmx/GetSpecialProducs', data).success(function(response) {
          $timeout(function() {deferred.resolve({ success: true, response: response })}, MOCK_ajaxDelay);  
       }).error(function() {
          $timeout(function() {deferred.resolve({ success: true, response: response } }, MOCK_ajaxDelay);  
       });
       return deferred.promise;
    }
    
    someService.someFunction(500).then(function(data) {
        if (data.success) {
          $scope.items = data.response.d;
        }
    });
    

    But if you are really mock testing, the better solution is to look into ngMock: http://docs.angularjs.org/api/ngMock.$httpBackend

提交回复
热议问题