2

当 API 在响应中返回某些代码时,我想显示一些通知。经过一些研究,我选择了这个模块,但我希望能够在 Restangular 的 responseInterceptor 中使用这些。这是我当前的拦截器..

RestangularProvider.setErrorInterceptor(function (response) {
    if (response.status !== 0) {
      return debouncedReportError(response);
    }
  });
  toastr.options = {
    'closeButton': true,
    'debug': true,
    'positionClass': 'toast-top-full-width',
    'onclick': null,
    'showDuration': '200',
    'hideDuration': '300',
    'timeOut': '200000',
    'extendedTimeOut': '1000',
    'showEasing': 'swing',
    'hideEasing': 'linear',
    'showMethod': 'fadeIn',
    'hideMethod': 'fadeOut'
  };

  reportError = function (response) {
    var errorOutput;

    if (response.status==403 && response.data.error.code>20000){
      Notification.success('Success notification');
      debugger;
    }
    if (typeof response.data === 'string') {
      return toastr.error('An Error occured: ' + response.statusText, 'Error: ' + response.status);
    } else if (response.data.error_message != null) {
      return toastr.error('' + response.data.error_message, 'Error: ' + response.status);
    } else if (response.data.error && response.data.error.errors != null && !_(response.data.error.errors).isEmpty()) {
      errorOutput = _(response.data.error.errors).reduce(function (memo, value, index) {
        return memo + value;
      }, '');
      return toastr.error('' + errorOutput, 'Error: ' + response.status);
    } else if (response.data.error && response.data.error.message != null) {
      return toastr.error('' + response.data.error.message, 'Error: ' + response.status);
    } else {
      return toastr.error('An error occured on the back end.', 'Error ' + response.status);
    }
  };
  return debouncedReportError = _.debounce(reportError, 2000, true);
}

我无法插入所需的服务“通知”。非常感谢任何帮助。多谢你们。

4

1 回答 1

1

根据此链接,您也可以在运行方法中配置restangular,因此您可以像下面这样配置它

angular.module('myApp')
  .run(function(Restangular, Notification) {
    Restangular.setErrorInterceptor(
      function(response) {        
        Notification.Show()
       }
        return elem;
    });
  });

虽然示例向您展示了请求拦截器,但您可以通过这种方式配置任何拦截器......

于 2015-03-04T16:05:37.890 回答