I have $scope.data
that is already displaying to the screen using ng-repeat
, I have a input where user can search from $scope.data
, So i created filter if user search text matched with $scope.data
highlight that text ,its not throwing any exception but also not highlighting the text, I am fairly new to angularjs filters help will be much appreciated. Thanks in advance.
ctrl.js
$scope.data = ["Lorem Ipsum is simply dummy text", "Lorem Ipsum is simply dummy text"];
main.html
<div class="row search-input-margin">
<div class="col-md-12 form-group">
<div class="col-md-3">
<label for="search">Search Logs:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" id="search" ng-model="vm.searchLog">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<ul style="list-style: none;">
<li ng-repeat="message in data track by $index"><span><strong>Log:</strong></span><span>{{message}}</span></li>
</ul>
</div>
<div>
<ul>
<!-- filter code -->
<div ng-repeat="item in data | filter:vm.searchLog"
ng-bind-html="item | highlight:vm.searchLog">
</div>
</ul>
</div>
</div>
filter.js
angular.module('App').filter('highlight', function($sce) {
return function(text, phrase) {
if (phrase) text = text.replace(new RegExp('('+phrase+')', 'gi'),
'<span class="highlighted">$1</span>')
return $sce.trustAsHtml(text)
}
});