AngularJS:如何从过滤器中获取 DOM 元素

AngularJS: How can I get the DOM element from a filter

问题如下: 我有几个应用过滤器 ("myFilter") 的 html 站点。

    angular.module('myApp', [])
    .filter('myFilter', function() {
            return function(input) {
                //do something with the input
                return input;
            };
    })

过滤器应用于没有特殊 ID 或 类 的不同标签,如下所示:

    <span>{{'some_string | myFilter'}}</span>

    <div>{{'some_other_sring' | myFilter}}</div>

我现在想向使用过滤器的元素添加 类 或样式(用于某些测试目的)。我的问题是,是否有办法在过滤器函数中获取 DOM 元素?目前我没有看到一个直接的方法来做这件事,并且会感谢关于如何实现它的具体或一般想法。 在此先感谢您的帮助。

P.S。 我知道我可以使用指令而不是过滤器,但是过滤器已经分布成吨 html 所以我更愿意使用过滤器。

使用过滤器无法做到这一点。请改用指令。例如:

angular.module('myApp', [])
.directive('myDirective', function() {
    return {
        link: function(scope, element, attributes) {
            element.addClass('some-class');
            ...
        }
    };
});

使用正则表达式 find/replace 向所有使用过滤器的元素添加指令并不难。