Highlighting a filtered result in AngularJS

前端 未结 13 2107
醉话见心
醉话见心 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:28

    In did that for AngularJS v1.2+

    HTML:

    
    

    JS:

    $scope.highlight = function(text, search) {
        if (!search) {
            return $sce.trustAsHtml(text);
        }
        return $sce.trustAsHtml(text.replace(new RegExp(search, 'gi'), '$&'));
    };
    

    CSS:

    .highlightedText {
        background: yellow;
    }
    

提交回复
热议问题