繁体   English   中英

在Angular.js中承诺

[英]Promise in Angular.js

我已经更改了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'},
        })
        };
       }]);

该函数可能不会以任何一种方式被调用。 这里的问题是,在第一种情况下,您没有将函数定义为回调,而是立即调用函数本身

$scope.commonAjaxErrorHandling("Failed to get  File data.",true)

这不是回调定义,而是$ scope.commonAjaxErrorHandling的立即调用

所以我的想法是没有错误发生,这就是为什么在第二种情况下错误函数都不被调用的原因

看来您正在错误地检查status 您应该访问fail回调的第二个参数:

  // ...
  ,
  function (jqXHR, textStatus, error) { 
     if(textStatus == 404){
        $scope.addErrorAlert("Service is down.Please try again later",true);
        return;
   }
 }

注意: 您要从Api服务返回的对象中没有file密钥,因此应改为flatFile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM