如何在使用 AngularJS 过滤之前显示加载消息?

How to show a loading message before filter with AngularJS?

我需要在 AngularJS 即将发生过滤器时显示一些 "Loading Message"。我的过滤器很快,所以我不能只在 return 过滤数据之前显示它。我的代码:

.filter('filterName', function () {
    return function (pacotes, escopo) {
        var filtered = [];
        pacotes.forEach(function (pacote) {
            if (condition)
                filtered.push(pacote);
        });

        return filtered;
    }
})

在 return 此数据之前,我可以做什么来显示一些消息,div,任何东西?

你的某处 html:

<p id="loadingMessage" ng-show="showMessage"></p>

过滤器:

.filter('filterName', function () {
    $scope.showMessage = true;

    $timeout(return function (pacotes, escopo) {
        var filtered = [];
        pacotes.forEach(function (pacote) {
            if (condition)
                filtered.push(pacote);
        });
        $scope.showMessage = false;
        return filtered;
    }, 3000);    
})

我无法对此进行测试,因为我没有您的所有代码。但是假设我的语法正确,这样的事情应该可以工作。不过,您必须在适当的位置注入 $scope 和 $timeout 才能使其正常工作。

编辑:我刚刚意识到 escopo 可能意味着另一种语言中的范围。假设这是真的,并且您实际上是在传递范围,只需将 escopo 放在我有 $scope 的地方。