【问题标题】:use angular-ui-notification with Restangular Interceptor将 angular-ui-notification 与 Restangular 拦截器一起使用
【发布时间】:2015-03-04 14:31:16
【问题描述】:

当 API 在响应中返回某些代码时,我想显示一些通知。经过一些研究,我选择了 this 模块,但我希望能够在 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);
}

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

【问题讨论】:

    标签: angularjs toastr


    【解决方案1】:

    按照link,你也可以在run方法中配置restangular,然后你可以像下面这样配置它

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2018-01-07
      相关资源
      最近更新 更多