【发布时间】: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