mark search string dynamically using angular.js

前端 未结 4 2204
北荒
北荒 2021-02-08 07:22

How can I mark my search pattern dynamically in my html?
Example:

\"SearchExample\"

I\'m using angular

4条回答
  •  忘掉有多难
    2021-02-08 07:47

    Angular UI is a great choice. You can also do it with filter like: http://embed.plnkr.co/XbCsxmfrgmdtOAeBZPUp/preview

    The essence is as commented by @Hylianpuffball, dynamically create styled 'span' tags for the matches.

    .filter('highlight', function($sce) {
      return function(text, phrase) {
        if (phrase) text = text.replace(new RegExp('('+phrase+')', 'gi'),
          '$1')
    
        return $sce.trustAsHtml(text)
      }
    })
    

    And use it like:

提交回复
热议问题