Banner Image
Banner Image
Banner Image

How to Highlight Search Results in AngularJS

Highlight Search Results in ng repeat using AngularJS:
Let us see how to highlight the search results in ng repeat using Angular filter. Highlighting the
search result improves user experience and minimizes the search time when compared showing the
search results.

Here is the process that explains how to highlight the strings in ng repeat data:
1.Include angular.js on your header section
[html]
<script src=" angular.min.js" type="text/javascript"></script> [/html]
2.Put below html structure on your html page
[html]
<div data-ng-app="SampleApp" data-ngcontroller="SampleCntrl">
<div>
<searchsection data="searchdata"></searchsection>
</div>
</div>
[/html]
3.Write custom style for class .highlighted
[html]
<style>
.highlighted { font-weight:bold; color:#3399cc; font-size:16px; }
</style>
[/html]
4.Paste the following script code on your page
[html]
<script>
var app = angular.module(‘SampleApp’, []);
app.controller(‘SampleCntrl’, function($scope) {
$scope.searchdata = [ // Sample Data
{
website:’sharesoft.in’,
companyname:’sharesoft’,
search_engine: ‘google’,
traffic: ’50’,
},
{
website:’webcodemonster.com’,
companyname:’webcodemonster’,
search_engine: ‘google’,
traffic: ’40’,
}
];
});
app.directive(‘searchsection’, function(){
return{
scope:{
searchdata: ‘=data’
},
template:’ <input type="text" id="search_result" data-ng-model="search.text"
class="inputText form-control form_control" placeholder="Search" ><ul class="search_visb"><li
data-ng-repeat="mysearch in searchdata | filter:website | filter:search.text"><a
class="sear_img_avis"><span class="S_com_name" data-ng-bind-html="mysearch.website |
highlight1:search.text">{{mysearch.website}}</span><span class="S_name_name"
data-ng-bind-html="mysearch.companyname |
highlight:search.text">{{mysearch.companyname}}</span></a></li></ul>’,
link:function(scope,element,attributes){

}
}
}
).filter(‘highlight’, function($sce) {

return function(cname, phrase) {
if (phrase) cname = cname.replace(new RegExp(‘(‘+phrase+’)’, ‘gi’),
‘<span class="highlighted">$1</span>’)
return $sce.trustAsHtml(cname)
}
}).filter(‘highlight1’, function($sce) {
return function(web, phrase) {
if (phrase) web = web.replace(new RegExp(‘(‘+phrase+’)’, ‘gi’),
‘<span class="highlighted">$1</span>’)
return $sce.trustAsHtml(web)
}
});
</script>
[/html]
Sample Output :-

Here is the brief explanation for the above coding:-

Custom Directive – performs the functionality of template’s “html” structure.
[html]
Here, “<searchsection”> </searchsection> is the custom directive.
[/html]
Let’s see how to declare for performing the function on template’s html structure with sample
data.

How to set the directive name in angular JS
[html]
app.directive(‘searchsection’, function(){} // Here Set directive name
[/html]
Sample Data:-
[html]
<searchsection data="searchdata"></searchsection>

Here, “searchdata” is the list of sample data’s for the search function.
[/html]
Template:-

It contains html structure with output of the directive function.

How to set the template in directive using angular js
[html]
app.directive(‘searchsection’, function(){
return{
scope:{
searchdata: ‘=data’
},
template:’My html ‘, // Set template
}

Filter: – Select a subset of items from an array.

if (phrase) cname = cname.replace(new RegExp(‘(‘+phrase+’)’, ‘gi’),
‘<span class="highlighted">$1</span>’)
return $sce.trustAsHtml(cname)
}
[/html]
Here, we have used “Regular Expression” for finding the string match, if your “typed string” matches
with the array of lists, the filter function just added “hightlight class” will be added to the string.
Depending upon the “hightlighted class” css, the output will be displayed in specified pattern.
Why use AngularJS for highlighting the search result over JQuery? It is that simple, Angular JS is the
emerging technology that allows creating applications very quickly and easily.

Blog

27/10/2025
The Rise of Atlas Browser: Why More Users Are Switching to a Privacy-First Web
In today’s digital world, almost everything we do — from searching for information to online…
24/10/2025
Understanding WordPress: A Simple Guide for Beginners in 2025
If you’ve ever wondered how people build websites without knowing how to code, the answer…
15/10/2025
Building a Robust eCommerce Website with Magento: A Complete Guide by Sharesoft
In today’s fast-moving digital world, building a strong online presence is more than just having…
11/10/2025
Why Custom Shopify Website Development Is the Key to Building a Scalable eCommerce Brand
Introduction: The Rise of Shopify in Modern eCommerce In today’s fast-paced digital marketplace, where customer…

Stay Ahead in Digital Innovation

Looking for expert tips, industry insights, and strategies to boost your digital presence? Subscribe to Sharesoft Technology’s weekly LinkedIn newsletter!