Luk*_*kmo 55 angularjs angularjs-ng-repeat
我正在使用ng-repeat并在angularJS中过滤,就像手机教程一样,但我想在页面中突出显示搜索结果.使用基本的jQuery,我只需要在输入上的键上解析页面,但我正在尝试以角度方式进行.有任何想法吗 ?
我的代码:
<input id="search" type="text" placeholder="Recherche DCI" ng-model="search_query" autofocus>
<tr ng-repeat="dci in dcis | filter:search_query">
<td class='marque'>{{dci.marque}} ®</td>
<td class="dci">{{dci.dci}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
Dmi*_*zin 90
为AngularJS v1.2 +做了那个
HTML:
<span ng-bind-html="highlight(textToSearchThrough, searchText)"></span>
Run Code Online (Sandbox Code Playgroud)
JS:
$scope.highlight = function(text, search) {
if (!search) {
return $sce.trustAsHtml(text);
}
return $sce.trustAsHtml(text.replace(new RegExp(search, 'gi'), '<span class="highlightedText">$&</span>'));
};
Run Code Online (Sandbox Code Playgroud)
CSS:
.highlightedText {
background: yellow;
}
Run Code Online (Sandbox Code Playgroud)
Uri*_*Uri 22
angular ui-utils只支持一个术语.我使用以下过滤器而不是范围函数:
app.filter('highlight', function($sce) {
return function(str, termsToHighlight) {
// Sort terms by length
termsToHighlight.sort(function(a, b) {
return b.length - a.length;
});
// Regex to simultaneously replace terms
var regex = new RegExp('(' + termsToHighlight.join('|') + ')', 'g');
return $sce.trustAsHtml(str.replace(regex, '<span class="match">$&</span>'));
};
});
Run Code Online (Sandbox Code Playgroud)
和HTML:
<span ng-bind-html="theText | highlight:theTerms"></span>
Run Code Online (Sandbox Code Playgroud)
Dmi*_*try 13
过滤器 - > Highlite(过滤器).还有Keypress指令.
的index.html
<!DOCTYPE html>
<html>
<head>
<script src="angular.js"></script>
<script src="app.js"></script>
<style>
.highlighted { background: yellow }
</style>
</head>
<body ng-app="Demo">
<h1>Highlight text using AngularJS.</h1>
<div class="container" ng-controller="Demo">
<input type="text" placeholder="Search" ng-model="search.text">
<ul>
<!-- filter code -->
<div ng-repeat="item in data | filter:search.text"
ng-bind-html="item.text | highlight:search.text">
</div>
</ul>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
app.js
angular.module('Demo', [])
.controller('Demo', function($scope) {
$scope.data = [
{ text: "<< ==== Put text to Search ===== >>" }
]
})
.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)
}
})
Run Code Online (Sandbox Code Playgroud)
参考:http://codeforgeek.com/2014/12/highlight-search-result-angular-filter/ 演示:http://demo.codeforgeek.com/highlight-angular/
angular-bootstrap中有标准的高光滤镜:typeaheadHighlight
用法
<span ng-bind-html="text | typeaheadHighlight:query"></span>
Run Code Online (Sandbox Code Playgroud)
使用范围{text:"Hello world", query:"world"}渲染
<span...>Hello <strong>world</strong></span>
Run Code Online (Sandbox Code Playgroud)
我希望我的光榜能让人们易于理解:
app.filter('highlight', function() {
return function(text, phrase) {
return phrase
? text.replace(new RegExp('('+phrase+')', 'gi'), '<kbd>$1</kbd>')
: text;
};
});
Run Code Online (Sandbox Code Playgroud)
<input type="text" ng-model="search.$">
<ul>
<li ng-repeat="item in items | filter:search">
<div ng-bind-html="item | highlight:search.$"></div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)