如果搜索文本与数组中的可用文本匹配,如何突出显示数据?
How to highlight data if search text matched with available text in the array?
我有 $scope.data
已经使用 ng-repeat
显示在屏幕上,我有一个输入,用户可以从 $scope.data
搜索,所以如果用户搜索文本匹配,我创建了过滤器使用 $scope.data
突出显示该文本,它不会抛出任何异常但也不会突出显示文本,我对 angularjs 过滤器帮助还很陌生,我们将不胜感激。提前致谢。
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"></span>')
return $sce.trustAsHtml(text)
}
});
<div ng-repeat="item in data | filter:vm.searchLog"
ng-bind-html-unsafe="item | highlight:vm.searchLog">
</div>
这里为什么不用ng-bind-html-unsafe
?如果您使用的是 ng-bind-html
,您是否正在加载 angular-sanitize.min.js
这里有更详细的link:
AngularJS : Insert HTML into view
您缺少 .highlighted
class
的样式
var app = angular.module("sa", []);
app.controller("FooController", function($scope) {
$scope.data = ["Lorem Ipsum is simply dummy text", "Lorem Ipsum is simply dummy"];
});
app.filter('highlight', function($sce) {
return function(text, phrase) {
if (phrase) text = text.replace(new RegExp('(' + phrase + ')', 'gi'),
'<span class="highlighted"></span>')
return $sce.trustAsHtml(text)
}
});
.highlighted {
font-weight: bold
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div ng-app="sa" ng-controller="FooController as vm">
<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>
<ul>
<!-- filter code -->
<li ng-repeat="item in data | filter: vm.searchLog" ng-bind-html="item | highlight:vm.searchLog">
</li>
</ul>
</div>
我有 $scope.data
已经使用 ng-repeat
显示在屏幕上,我有一个输入,用户可以从 $scope.data
搜索,所以如果用户搜索文本匹配,我创建了过滤器使用 $scope.data
突出显示该文本,它不会抛出任何异常但也不会突出显示文本,我对 angularjs 过滤器帮助还很陌生,我们将不胜感激。提前致谢。
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"></span>')
return $sce.trustAsHtml(text)
}
});
<div ng-repeat="item in data | filter:vm.searchLog"
ng-bind-html-unsafe="item | highlight:vm.searchLog">
</div>
这里为什么不用ng-bind-html-unsafe
?如果您使用的是 ng-bind-html
,您是否正在加载 angular-sanitize.min.js
这里有更详细的link: AngularJS : Insert HTML into view
您缺少 .highlighted
class
var app = angular.module("sa", []);
app.controller("FooController", function($scope) {
$scope.data = ["Lorem Ipsum is simply dummy text", "Lorem Ipsum is simply dummy"];
});
app.filter('highlight', function($sce) {
return function(text, phrase) {
if (phrase) text = text.replace(new RegExp('(' + phrase + ')', 'gi'),
'<span class="highlighted"></span>')
return $sce.trustAsHtml(text)
}
});
.highlighted {
font-weight: bold
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div ng-app="sa" ng-controller="FooController as vm">
<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>
<ul>
<!-- filter code -->
<li ng-repeat="item in data | filter: vm.searchLog" ng-bind-html="item | highlight:vm.searchLog">
</li>
</ul>
</div>