Highlighting a filtered result in AngularJS

前端 未结 13 2084
醉话见心
醉话见心 2020-12-01 00:12

I\'m using a ng-repeat and filter in angularJS like the phones tutorial but I\'d like to highlight the search results in the page. With basic jQuery I would have simply pars

13条回答
  •  無奈伤痛
    2020-12-01 00:32

    My solution for highlight, used this with angular-ui-tree element: https://codepen.io/shnigi/pen/jKeaYG

    angular.module('myApp').filter('highlightFilter', $sce =>
     function (element, searchInput) {
       element = element.replace(new RegExp(`(${searchInput})`, 'gi'),
                 '$&');
       return $sce.trustAsHtml(element);
     });
    

    Add css:

    .highlighted {
      color: orange;
    }
    

    HTML:

    And to add search input:

    
    

提交回复
热议问题