0

I have a simple AngularJS app which consumes a JSON array of objects like this:

$scope.faq = [
 { id: 1, question: "What is the meaning of life?", answer: "42"},
 { id: 2, question: "What <em>is<em> 1+1?", answer: "4"}
]

In my HTML I have an ng-repeat with a basic filter coming from a text input like this

ng-repeat="qa in faq | filter:searchText"

The problem is I want the search to filter while ignoring the HTML tags in the JSON objects, so that searching for the phrase "what is" will return both objects instead of just the first one. How do I change the filter to do this?

4

2 回答 2

2

编写您的自定义过滤器

在 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) {

于 2013-08-23T20:48:20.573 回答
0

我敢肯定有更好的方法,但至少这是相当简单的:

return $('<div>').html(value).text();

非 jQuery 版本:

var div = document.createElement('div');
div.innerHTML = value;
return div.innerText;

使用其中任何一个创建一个过滤器并将其放在您的filter:searchText.

于 2013-08-23T20:39:22.020 回答