I want to mark the words that I have in the array.
$scope.arrayFilter=["mom","is","beautifull"];
But this only works for me if I have the words in the order in which they appear. I wish that regardless of the order in which these words are marked, if they match. And if I add a new word to the array, it should also be marked.
https://jsfiddle.net/1x7zy4La/
<li ng-repeat="item in data ">
<span ng-bind-html="item.title | highlight:arrayFilter"></span>
</li>
$scope.arrayFilter=["mom","is","beautifull"];
$scope.data = [{
title: "mom is beautifull"
}, {
title: "my mom is great"
}, {
title: "I hate the matematics"
}];
});
app.filter('highlight', function($sce) {
return function(text, arrayFilter) {
var stringToDisplay = '';
angular.forEach(arrayFilter,function(key,value){
if(text.includes(key)){
stringToDisplay = stringToDisplay.concat(key).concat(" ");
}
})
stringToDisplay = stringToDisplay.substring(0, stringToDisplay.length - 1);
return $sce.trustAsHtml(text.replace(new RegExp(stringToDisplay, 'gi'), '<span class="highlightedText">$&</span>'));
}
});

source
share