我已将输入文本值存储到 id name=search-query 的变量中。然后我正在搜索 JSON 数据以找到任何匹配的结果,然后将结果输出到屏幕上。有没有一种方法可以加粗匹配 search-query.val 的单词?
<body ng-app="products" ng-controller="ctrl">
<input type="text" id="search-query" name="query" placeholder="Enter product name"></input>
<tbody>
<tr ng-repeat="result in searchResult|orderBy:order:reverse" >
<td >
<a ng-href="{{result.link}}" target="_blank">
<span ng-bind="result.name" target="_blank"></span>
</a>
</td>
<td ng-bind="result.type"></td>
</tr>
</tbody>
</body>
var app2 = angular.module('products', []);
app2.controller('ctrl', function($scope) {
$scope.searchResult = [];
$scope.submit = function(){
var search = $("#search-query").val();
$.each(json.products, function(i, v) {
var regex = new RegExp(search, "i");
if (v.name.search(regex) != -1) {
// For the following line, is there a way which I can bold the word which match the search-query.val?
var name = v.name.replace(search, "<b>search</b>"); // doesn't work
$scope.searchResult.push({ name:name, type: v.type, link: v.url });
return;
}
});
}