我有一个简单的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”将返回两个对象而不是第一个对象。如何更改过滤器以执行此操作?
答案 0 :(得分:2)
编写自定义过滤器
in 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 in faq | customFilter:searchString"
js:return function(input, searchString) {
答案 1 :(得分:0)
我确信有更好的方法,但至少这很简单:
return $('<div>').html(value).text();
非jQuery版本:
var div = document.createElement('div');
div.innerHTML = value;
return div.innerText;
使用其中任何一个创建过滤器,并将其放在filter:searchText
。