如何在特定请求上禁用或停止Angular BlockUI

时间:2015-07-08 16:45:36

标签: angularjs blockui

我已经在任何请求上自动启用了BlockUI show,但我需要关闭特定请求。我需要这样的东西。

$scope.getTags = function ($query) {
            blockUI.notShow();
            return blogService.getTags($query)
        };

2 个答案:

答案 0 :(得分:8)

我尝试了一些工作。

1)停止自动阻止

app.config(["$routeProvider", "blockUIConfig", function ($routeProvider, blockUIConfig) {
    blockUIConfig.autoBlock = false;
}]);

2)创建一个拦截器

app.config(["$httpProvider", function ($httpProvider) {
    $httpProvider.interceptors.push('myInterceptorService');
}]);
app.factory('myInterceptorService', ['$q', '$injector','blockUI',
    function ($q, $injector, blockUI) {

        var myInterceptorServiceFactory = {};

        myInterceptorServiceFactory.request = function (config) {
            if (blockUI.noOpen == null) {
            blockUI.stop();
        } else {
            blockUI.noOpen = null;
        }
            return config;
        }
        myInterceptorServiceFactory.responseError = function (rejection) {
            return $q.reject(rejection);
        }
        myInterceptorServiceFactory.response = function (response) {
            if (blockUI.noOpen == null) {
            blockUI.stop();
        } else {
            blockUI.noOpen = null;
        }
            return response || $q.when(response);
        }

        return myInterceptorServiceFactory;
    }
]);

3)控制器中的警报

$scope.getTags = function ($query) {
            blockUI.noOpen = true;
            return blogService.getTags($query)
        };

答案 1 :(得分:1)

使用McNull AngularJS Block UI模块...您可以使用blockUIConfig.requestFilter函数过滤掉特定请求...

angular.module('yourAppModule').config(["blockUIConfig", function (blockUIConfig) {
    blockUIConfig.requestFilter = function (config) {
    //Perform a global, case-insensitive search on the request url for 'noblockui' ...
    if (config.url.match(/noblockui/gi)) {
        return false; // ... don't block it.
    }
};}]);

在特定请求上只需将以下内容添加到网址...

url = backendAddress + "/?noblockui";
return $http.get(url);