目前我有一个代码,如果与此数组匹配,则会突出显示列表中的单词。
$scope.arrayFilter=["is","mom","beautifull",'beer'];
我不再需要这段代码。我只需要强调班级的文本" .marque"从数组中不会失去执行" marquee"的库的效果影响。我怎么能这样做?
<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);
}
});
答案 0 :(得分:1)
您可以向filter
添加第二个参数,以指示这样的选框元素:
<div class='marquee' ng-bind-html="text | highlight:arrayFilter:true"></div>
在内filter
你可以检查这个标志并应用选框:
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>