如何报告来自AngularJS $ q.all的错误?

时间:2014-11-01 14:47:13

标签: angularjs

我的代码如下所示:

    $q.all([
        this.getData(EnumGetData.ContentStatus),
        this.getData(EnumGetData.ContentType),
        this.getData(EnumGetData.UserProfile),
    ])
        .then((results) => {
            self.contentStatus = results[1];
            self.contentType = results[2];
            self.userProfile = results[3];
        }, function () {
            alert // 
        });

我的getData函数参数如下:

        getData(controller) {

如果出现错误,我的getData函数会返回:

        .error((data, status, headers, config) {
            defer.reject();
        });

我想做的是在$ q.all失败时给出警告消息。任何人都可以建议我如何做到这一点。

1 个答案:

答案 0 :(得分:0)

这是一个了解你需要做什么http://plnkr.co/KUPTjpbVBgMhWUWrh1YI

您可以传递这样的错误消息:deferred.reject('ERROR MESSAGE')然后您可以在当时的错误处理程序中捕获它。

function getData(input) {
    var deferred = $q.defer();

    $timeout(function() {
      deferred.reject(input);
    }, 2000);

    return deferred.promise;
  }

  $q.all([getData(1),
    getData(2),
    getData(3)
  ]).then(function success(data) {
    console.log('should not get here');
  }, function(error) {
    alert(error);
  });