简体   繁体   English

在Angular.js中承诺

[英]Promise in Angular.js

I have changed the env property of my url to test my code locally.. It seems the error call back is not even getting called. 我已经更改了URL的env属性以在本地测试我的代码。似乎错误回调甚至没有被调用。 My code snippet- 我的代码段

        $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..
           );
         };

If I try writing other error function. 如果我尝试编写其他错误函数。 It is not even getting called. 它甚至没有被调用。

        $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;
     }
 });
};

Can someone help me out this situation? 有人可以帮我解决这种情况吗?

        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'},
        })
        };
       }]);

The function probably doesn't get called in either way. 该函数可能不会以任何一种方式被调用。 The problem here is that in the first case you do not define a function as a callback but immediately call the function itself 这里的问题是,在第一种情况下,您没有将函数定义为回调,而是立即调用函数本身

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

this is not a callback-definition but the immediate call of $scope.commonAjaxErrorHandling 这不是回调定义,而是$ scope.commonAjaxErrorHandling的立即调用

So my idea is that no error occurs and thats why in the second case the error-function isn't called either 所以我的想法是没有错误发生,这就是为什么在第二种情况下错误函数都不被调用的原因

It seems like you are checking for the status incorrectly. 看来您正在错误地检查status You should be accessing the second parameter of the fail callback: 您应该访问fail回调的第二个参数:

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

Note: There is no file key in the object that you're returning from Api service, and therefore it should be flatFile instead. 注意: 您要从Api服务返回的对象中没有file密钥,因此应改为flatFile

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

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