【问题标题】:Highlight the text of a span in angularjs在angularjs中突出显示跨度的文本
【发布时间】:2018-02-06 08:18:18
【问题描述】:

目前我有一个代码,如果与此数组匹配,则突出显示列表中的单词。

  $scope.arrayFilter=["is","mom","beautifull",'beer'];

我不再需要这段代码。我只需要从数组中突出显示类“.marque”的文本,而不会失去执行“选框”效果的库的效果。我该怎么做?

https://jsfiddle.net/mafa4hro/

<div ng-app="testApp" ng-controller="testCtrl">
  <li ng-repeat="item in data ">
    <span ng-bind-html="item.title | highlight:arrayFilter"></span>
  </li>
  <div class='marquee' >mom is beautifull</div>
</div>

  var app = angular.module('testApp',[]);
  app.controller('testCtrl',function($scope){
  $scope.arrayFilter=["mom","is","beautifull",'beer'];
  $scope.data = [{
  title: "mom is beautifull"
  }, {
  title: "my mom is great"
  }, {
  title: "I hate the matematics"
  }];

  //marquee effect
    $('.marquee').marquee({
      duration: 5000
    });

    $('.marquee')
    .bind('finished', function(){
    console.log('finish')
      $(this).html('If it works, i need a beer')
        //Apply marquee plugin again
        .marquee({
          duration: 5000,
        })
    })

  });

app.filter('highlight', function($sce) {
 return function(text, arrayFilter) {
 angular.forEach(arrayFilter, function(key, value) {
 if (text.includes(key)) {
    text = text.replace(new RegExp(key, 'gi'), '<spanclass="highlightedText">$&</span>')
   }
  })
  return $sce.trustAsHtml(text);

  }
 });

【问题讨论】:

  • 如果您只想为您的标记文本着色,您可以在您的.marquee 中添加color:'color_name'
  • @AkashKC 那么你如何高亮匹配数组中的单词呢?
  • @yavg 类为marquee 的元素在数组之外,因此不会对其应用过滤器...请详细说明问题?跨度>
  • @kukkuz 你是什么意思“在数组之外”,我该如何解决?
  • 对不起,我看错了...请在下面找到我的回复...

标签: javascript jquery angularjs filter angular-filters


【解决方案1】:

您可以将 second 参数添加到 filter指示这样的选取框元素:

<div class='marquee' ng-bind-html="text | highlight:arrayFilter:true"></div>

并且filterfilter你可以检查这个标志并应用选框:

if (isMarquee)
    $('.marquee').marquee({duration: 5000});

见下面的演示和updated jsfiddle:

var app = angular.module('testApp', []);
app.controller('testCtrl', function($scope) {
  $scope.arrayFilter = ["is", "mom", "beautifull", 'beer', 'hate'];
  $scope.data = [{
    title: "mom is beautifull"
  }, {
    title: "my mom is great"
  }, {
    title: "I hate the matematics"
  }];

  $scope.text = 'If it works, i need a beer';
});

app.filter('highlight', function($sce) {
  return function(text, arrayFilter, isMarquee) {
    angular.forEach(arrayFilter, function(key, value) {
      if (text.includes(key)) {
        text = text.replace(new RegExp(key, 'gi'), '<span class="highlightedText">$&</span>')
      }
    })
    if (isMarquee)
      //marquee
      $('.marquee').marquee({
        duration: 5000
      });
    return $sce.trustAsHtml(text);

  }
});
.highlightedText {
  background: yellow;
}

.marquee {
  width: 300px;
  overflow: hidden;
  border: 1px solid #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.Marquee/1.3.94/jquery.marquee.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>



<div ng-app="testApp" ng-controller="testCtrl">
  <li ng-repeat="item in data ">
    <span ng-bind-html="item.title | highlight:arrayFilter"></span>
  </li>

  <div class='marquee' ng-bind-html="text | highlight:arrayFilter:true">mom is beautifull</div>
</div>

【讨论】:

  • 伟人!如果我实时更新选框的文字,找到的话会高亮吗?
猜你喜欢
  • 2018-10-02
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2019-07-23
  • 1970-01-01
  • 2011-09-25
相关资源
最近更新 更多