我有一个简单的 AngularJS 应用程序,它使用如下所示的 JSON 对象数组:
$scope.faq = [
{ id: 1, question: "What is the meaning of life?", answer: "42"},
{ id: 2, question: "What <em>is<em> 1+1?", answer: "4"}
]
在我的 HTML 中,我有一个 ng-repeat,它带有来自这样的文本输入的基本过滤器
ng-repeat="qa in faq | filter:searchText"
问题是我希望过滤搜索,同时忽略 JSON 对象中的 HTML 标记,以便搜索短语“what is”将返回两个对象,而不仅仅是第一个对象。如何更改过滤器才能实现此目的?
最佳答案
编写您的自定义过滤器
在 html 中:
ng-repeat="qa in faq | customFilter"
在js中:
angular.module('youModule', []).
filter('customFilter', function() {
return function(input) {
var out;
//parse your strings(find <tag> and exclude it) and push in out variable
return out;
}
});
UPD
您不仅可以在自定义过滤器中传输输入值(您可以添加参数,例如“searchText”)
html: ng-repeat="常见问题解答中的 qa | customFilter:searchString"
js:返回函数(输入,搜索字符串){
关于javascript - 在 AngularJS 中过滤没有 HTML 标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18411324/