AngularJS Service Config value get destroyed on minification

后端 未结 3 1657
情话喂你
情话喂你 2021-01-11 11:02

Having some trouble with minification and AngularJS ;-(

I found this jsfiddle \"loading\" extender for HTTP request, through the AngularJS Wiki page.

It work

3条回答
  •  别那么骄傲
    2021-01-11 11:34

    Use inline annotation for defining providers:

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

    And, btw, you should reconsider using jQuery calls inside your configs and factories. Direct DOM manipulation should be handled inside the directives.

    For your case, instead of $("#loader").show(); and $("#loader").show(); you should broadcast an event (e.g. $rootScope.$broadcast('loader_show')), and then listen for that event in your custom 'spinner' directive:

    HTML:

    JS:

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

提交回复
热议问题