缩小和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);
});
};
});
Run Code Online (Sandbox Code Playgroud)
缩小代码:
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)
})
}
});
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误: 错误:未知提供程序:来自app.services
Ste*_*wie 33
使用内联注释来定义提供者:
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);
}
]
);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你应该重新考虑在你的配置和工厂中使用jQuery调用.应该在指令内处理直接DOM操作.
对于你的情况,而不是$("#loader").show();
和$("#loader").show();
你应该广播的事件(例如$rootScope.$broadcast('loader_show')
),然后听您的自定义"微调"指令,事件:
HTML:
<div spinner class="loader"></div>
Run Code Online (Sandbox Code Playgroud)
JS:
app.directive('spinner',
function() {
return function ($scope, element, attrs) {
$scope.$on('loader_show', function(){
element.show();
});
$scope.$on('loader_hide', function(){
element.hide();
});
};
}
);
Run Code Online (Sandbox Code Playgroud)