如何使用angularjs过滤器突出显示多个搜索关键字?

时间:2017-06-16 14:30:24

标签: javascript angularjs angular-filters

一旦用户获得结果,我想在结果中突出显示搜索关键字,当用户搜索一个关键字时突出显示但是当搜索多个关键字时,它仍然只突出显示一个搜索关键字。

如何使用以下过滤器突出显示所有搜索关键字。

filter.js

  angular.module('loggingApp').filter('trusted', function($sce) {
      return function(text, phrase) {

      console.log("phrase",phrase);
        if (phrase) {
          var data = text.replace(/[|&;$%@"<>()+,]/g, "");
          data = data.replace(new RegExp('(' + phrase + ')', 'gi'),
            '<span class="highlighted">$1</span>');
          return $sce.trustAsHtml(data);
        }

        text = text.replace(/[|&;$%@"<>()+,]/g, "");
        return $sce.trustAsHtml(text);
     };
    });

main.html中

<tr ng-repeat="item in showMessages | filter:searchStr" >
                    <td >{{item.filename}}</td>
                    <td class="serverResults"  ng-bind-html="item.value | trusted:searchStr">{{item.value}}</td>
                </tr>

mainCtrl.js

$scope.$on('displaySearchResults',function(e,data){
        $scope.searchStr = data.searchStr;
        $scope.showMessages = data.messageObj;

    });

数据文字和短语

In below example i passed multiple strings ["3c72a612-4879-4877-b415-bf89ab110630","125981"] but it only highlight last one 125981.also from ctrl it sends one phrase `125981` as you see in phrase output from filter.

Text

[2017-06-16T02:48:36-04:00]zld00826infon/a[routes.event] ########## Message added to processing queue ########## 3c72a612-4879-4877-b415-bf89ab110630 { workerId: 6 pid: 125981 }

Phrase

125981

0 个答案:

没有答案