我希望在子视图加载到ng-view中时显示ajax指示符。我想只为路由器显示它,但下面的代码片段显示了所有ajax请求的ajax指示符,如何修复它?
directive('loading', ['$http', function($http) {
return {
restrict: 'A',
link: function (scope, elm, attrs)
{
debugger;
scope.isLoading = function () {
debugger;
return $http.pendingRequests.length > 0;
};
scope.$watch(scope.isLoading, function (v)
{
debugger;
if(v){
elm.show();
}else{
elm.hide();
}
});
}
};
}
]);
用法
<div class="ajax-ngview" data-loading></div>
<div ng-view></div>
我看起来像
$routeProvider
.when('/', {
templateUrl: '/MCT/home/home', //when this url is being loaded show a particular ajax activity gif
controller: function($scope) {
$scope.setActiveMenuMod('mct-mod');
}
})
答案 0 :(得分:0)
一种策略是您可以使用http拦截器拦截$ http请求并对其做出反应
https://docs.angularjs.org/api/ng/service/$http
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
//trigger data-loading
},
'response': function(response) {
// same as above
}
};
});