AngularJS Service Config值在缩小时被破坏

时间:2013-03-11 14:50:49

标签: angularjs minify

在缩小和AngularJS方面遇到一些麻烦; - (

我通过AngularJS Wiki页面找到了此jsfiddle“加载”扩展程序的HTTP请求。

直到我发表它才有效,并且缩小了它。 我找不到在配置上使用“注入”的方法,所以我很遗憾该怎么办。

原始代码:

angular.module("app.services", []).config(function($httpProvider) {
  var spinnerFunction;
  $httpProvider.responseInterceptors.push("myHttpInterceptor");
  spinnerFunction = function(data, headersGetter) {
    $("#loader").show();
    return data;
  };
  return $httpProvider.defaults.transformRequest.push(spinnerFunction);
}).factory("myHttpInterceptor", function($q, $window) {
  return function(promise) {
    return promise.then((function(response) {
      $("#loader").hide();
      return response;
    }), function(response) {
      $("#loader").hide();
      return $q.reject(response);
    });
  };
});

缩小代码:

angular.module("app.services", []).config(function (a) {
    var b;
    a.responseInterceptors.push("myHttpInterceptor");
    b = function (d, c) {
        $("#loader").show();
        return d
    };
    return a.defaults.transformRequest.push(b)
}).factory("myHttpInterceptor", function (a, b) {
    return function (c) {
        return c.then((function (d) {
            $("#loader").hide();
            return d
        }), function (d) {
            $("#loader").hide();
            return a.reject(d)
        })
    }
});

会抛出以下错误: 错误:未知提供商:来自app.services

3 个答案:

答案 0 :(得分:30)

使用inline annotation来定义提供者:

angular.module("app.services", [])
  .config(
    [
      '$httpProvider',
      'myHttpInterceptor',
      function($httpProvider, myHttpInterceptor) {
        var spinnerFunction;
        $httpProvider.responseInterceptors.push(myHttpInterceptor);
        spinnerFunction = function(data, headersGetter) {
         $("#loader").show();
         return data;
        };
        return $httpProvider.defaults.transformRequest.push(spinnerFunction);
      }
    ]
  );

顺便说一句,你应该重新考虑在你的配置和工厂中使用jQuery调用。应该在指令内处理直接DOM操作。

对于您的情况,您应该广播一个事件(例如$("#loader").show();),而不是$("#loader").show();$rootScope.$broadcast('loader_show'),然后在您的自定义'spinner'指令中监听该事件:

HTML:

<div spinner class="loader"></div>

JS:

app.directive('spinner',
    function() {
      return function ($scope, element, attrs) {
        $scope.$on('loader_show', function(){
          element.show();
        });

        $scope.$on('loader_hide', function(){
          element.hide();
        });
      };

    }

  );

答案 1 :(得分:3)

对于处于相同情况的其他人......我遵循了@Stewie的建议,取而代之的是,它只使用AngularJS代码,没有愚蠢的jQuery依赖; - )

<强>服务

app.config([
  "$httpProvider", function($httpProvider) {
    var spinnerFunction;
    $httpProvider.responseInterceptors.push("myHttpInterceptor");
    spinnerFunction = function(data, headersGetter) {
      return data;
    };
    return $httpProvider.defaults.transformRequest.push(spinnerFunction);
  }
]).factory("myHttpInterceptor", [
  "$q", "$window", "$rootScope", function($q, $window, $rootScope) {
    return function(promise) {
      $rootScope.$broadcast("loader_show");
      return promise.then((function(response) {
        $rootScope.$broadcast("loader_hide");
        return response;
      }), function(response) {
        $rootScope.$broadcast("loader_hide");
        $rootScope.network_error = true;
        return $q.reject(response);
      });
    };
  }
]);

<强>指令

app.directive("spinner", function() {
  return function($scope, element, attrs) {
    $scope.$on("loader_show", function() {
      return element.removeClass("hide");
    });
    return $scope.$on("loader_hide", function() {
      return element.addClass("hide");
    });
  };
});

答案 2 :(得分:3)

尽管看起来很奇怪,但你也可以使用内联注释来实际.push()注入你的依赖关系$q$window,而不是使用函数()进入$httpProvider.responseInterceptors推送数组:

app.config(["$httpProvider", function($httpProvider) {
    $httpProvider.responseInterceptors.push(['$q', '$window', function($q, $window) {
        return function(promise) {
            return promise.then(function(response) {
                    $("#loader").hide();
                    return response;
                },
                function(response) {
                    $("#loader").hide();
                    return $q.reject(response);
                });
        };
    }]);
}]);
相关问题