[英]AngularJS: How can I get the DOM element from a filter
The problem is as follows: I have got several html sites where a filter ("myFilter") is applied. 问题如下:我有几个HTML站点,其中应用了过滤器(“ myFilter”)。
angular.module('myApp', [])
.filter('myFilter', function() {
return function(input) {
//do something with the input
return input;
};
})
The filter is applied within different tags with no special id's or classes, like so: 过滤器应用于没有特殊ID或类的不同标签中,如下所示:
<span>{{'some_string | myFilter'}}</span>
or 要么
<div>{{'some_other_sring' | myFilter}}</div>
I would now like to add classes or styles to the elements in which the filter is used (for some testing purposes). 我现在想为使用过滤器的元素添加类或样式(出于某些测试目的)。 My question is, if there is a way to get the DOM elements in the filter function?
我的问题是,是否有一种方法可以在过滤器函数中获取DOM元素? At the moment I don't see a straight forward way of doing it and would be thankful for a specific or general idea of how to achieve it.
目前,我还没有一个直截了当的实现方式,并且对于实现该目标的具体或一般性的想法表示感谢。 Thanks in advance for any help.
在此先感谢您的帮助。
PS I know I could use a directive instead of the filter but, the filter is already distributed in tons of html so I would prefer to work with the filter. PS我知道我可以使用指令代替过滤器,但是,过滤器已经以html形式分发,因此我更喜欢使用过滤器。
There is no way to do that using filter. 没有办法使用过滤器做到这一点。 Use directive instead.
使用指令代替。 For example:
例如:
angular.module('myApp', [])
.directive('myDirective', function() {
return {
link: function(scope, element, attributes) {
element.addClass('some-class');
...
}
};
});
Using regex find/replace it won't be too hard to add a directive to all elements that use the filter you wanted to accomplish this with. 使用正则表达式查找/替换,将指令添加到使用您要实现此目标的过滤器的所有元素并不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.