Angular watching for changes in $http.pendingRequests from directive code example

Example: Angular watching for changes in $http.pendingRequests from directive

scope.isBusy = function () {
    return $http.pendingRequests.length > 0;
};
scope.$watch(scope.isBusy, function (hasPending) {
    if (hasPending) {
      elm.show();
    } else {
      elm.hide();
    }
});

You may want to put this in a directive like this:

.directive('loadingBoxDir', ['$http', function ($http) {
    return {
       restrict: 'A',
       link: fn_link
    };
    function fn_link(scope, elm) {
       scope.isBusy= function () {
           return $http.pendingRequests.length > 0;
       };
       scope.$watch(scope.isBusy, function (hasPending) {
           if (hasPending) {
              elm.show();
           } else {
              elm.hide();
           }
       });
    }
}]);