【问题标题】:Highlighting with angularjs and $sce使用 angularjs 和 $sce 突出显示
【发布时间】:2014-10-05 10:30:24
【问题描述】:

请看下图:

很好。

现在,请看下图:

这对我不好。

我需要先强调以下语句:

声明:as

那么下面的语句:

声明:dad

它们都被突出显示。怎么做?

js代码:

tableService.highlight = function (text, search) {
                if (!search) {
                    return $sce.trustAsHtml(text);
                }
                var trustAsHtml = $sce.trustAsHtml(text.replace(new RegExp(search, 'gi'), '<span class="highlightedText">$&</span>'));

                return trustAsHtml;
            };

cshtml代码:

<td ng-hide="$index == tableService.choiseDeleteRecord ||$index==tableService.choiseUpdateRecord">
                <span ng-bind-html="tableService.highlight(newsCategory.NameFa, pagerService.search)"> {{newsCategory.NameFa}}</span>
            </td>
            <td class="direction-left" ng-hide="$index == tableService.choiseDeleteRecord ||$index==tableService.choiseUpdateRecord">
                <span ng-bind-html="tableService.highlight(newsCategory.NameFa, pagerService.search)"> {{newsCategory.NameEn}}</span>
            </td>

【问题讨论】:

    标签: javascript angularjs highlighting


    【解决方案1】:

    我试过了,我成功了。但我的代码可能不是最优的。

    如果你有更好的方法请分享

    js代码:

      tableService.highlight = function (text, search) {
            if (!search) {
                return $sce.trustAsHtml(text);
            }
            var splitSearch = search.split(' ');
            $.each(splitSearch, function (index, value) {
                text = replaceAll(value, '#####$&~~~~~', text);
            });
            text = replaceAll('#####', '<span class="highlightedText">', text);
            text = replaceAll('~~~~~', '</span>', text);
            var trustAsHtml = $sce.trustAsHtml(text);
            return trustAsHtml;
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2016-02-24
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多