承诺Angular.js中的失败场景

时间:2016-01-08 10:46:49

标签: angularjs angularjs-directive angularjs-scope

我已经更改了我的url的env属性以在本地测试我的代码..似乎错误回调甚至没有被调用。 我的代码片段 -

            $scope.getfiles = function() {
            Api.file.query().$promise.then(
            function(result){ $scope.getfiles = result; },  //on success
            $scope.commonAjaxErrorHandling("Failed to get  File data.",true)  //on failure--> Always this line of  code is executing..
               );
             };

如果我尝试编写其他错误功能。它甚至没有被称为

       $scope.getfiles = function(){
        Api.flatFile.query().$promise.then(
         function(result){ $scope.File = result; 
      },
     function (result) { // this block is not getting called
         if(result.status== 404){
             $scope.addErrorAlert("Service is down.Please try again later",true);
             return;
         }
     });
    };

有什么理由吗?我也尝试了捕捉......但是工作很有效。

            FileServices.factory('Api', ['$res', '$loc',
            function($res, $loc){
            var contextPath = $loc.absUrl().split('/app/')[0];
            return {
            flatFile: $res(contextPath+'/app/config/flatFile/data', {}, {
            query: {method:'GET', isArray:true},
            save: {method:'POST'},
            })
            };
           }]);

1 个答案:

答案 0 :(得分:0)

将您的代码更改为以下内容,并告知我们是否正常运行。我不明白为什么你在那里使用$promise

$scope.getfiles = function() {
    // This service's function returns a promise
    Api.flatFile.query()
        .then(function(data) {
            // promise fulfilled                        
          }, function(error) {
            // promise rejected, could log the error with: console.log('error', error);
        });
};