[能否请您以黄色突出显示搜索到的单词?
下面编写了一个代码示例,用于过滤来自JSON feed URL的显示数据中的单词。
angular.module('sample', []).
controller('sampleController', ['$scope', '$http', function($scope, $http) {
var url = "https://spreadsheets.google.com/feeds/list/153Obe1TdWlIPyveZoNxEw53rdrghHsiWU9l-WgGwCrE/od6/public/values?alt=json";
$http.get(url)
.success(function(data, status, headers, config) {
$scope.users = data.feed.entry;
console.log($scope.users);
})
.error(function(error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
$scope.search = '';
$scope.searchFilter = function(item) {
if (item.gsx$topic.$t.indexOf($scope.search) != -1 || item.gsx$response.$t.indexOf($scope.search) != -1) {
return true;
}
return false;
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular.min.js"></script>
<div ng-app="sample" ng-controller="sampleController">
<div class="black">
<input type="text" name="search" ng-model="search" placeholder="search" ng-click="didSelectLanguage()" />
</div>
<br>
<br>
<br>
<table style="border: 1px solid black ;">
<tbody>
<tr>
<td>
<center><b>Question</b></center>
</td>
<td>
<center><b>Response</b></center>
</td>
</tr>
<tr ng-repeat="user in users | filter:searchFilter">
<td style="border: 1px solid black ; width:30%;white-space: pre-wrap;">{{user.gsx$topic.$t}}</td>
<td style="border: 1px solid black ; width:70%;white-space: pre-wrap;">{{user.gsx$response.$t}}</td>
</tr>
</tbody>
</table>
</div>
您需要使用$sce
服务Strict Contextual Escaping。
在控制器声明中添加此服务,如下所示:
controller('sampleController', ['$scope', '$http', '$sce', function($scope, $http, $sce) {
[现在,您必须定义一个函数,该函数将只注入带有黄色的CSS类名称的span
标记,以黄色突出显示,当找到搜索到的文本时,通过$sce.trustAsHtml
方法指示AngularJS,注入的是安全内容。
$scope.highlightText = function(text, search) {
if (search && search.length === 0) {
// Returns the default content.
return $sce.trustAsHtml(text);
}
// Define a regular expression to find the text globally and ignoring capital letters.
var regex = new RegExp(search, 'gi');
// If you already found the text then inject a span element with CSS class to highlight that you found.
return $sce.trustAsHtml(text.replace(regex, '<span class="foundText">$&</span>'));
};
在先前的正则表达式替换文本中,$&
指示显示与替换后的正则表达式匹配的捕获文本。
在此示例中查看:
angular.module('sample', []).
controller('sampleController', ['$scope', '$http', '$sce', function($scope, $http, $sce) {
var url = "https://spreadsheets.google.com/feeds/list/153Obe1TdWlIPyveZoNxEw53rdrghHsiWU9l-WgGwCrE/od6/public/values?alt=json";
$http.get(url)
.success(function(data, status, headers, config) {
$scope.users = data.feed.entry;
console.log($scope.users);
})
.error(function(error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
$scope.search = '';
$scope.searchFilter = function(item) {
if (item.gsx$topic.$t.indexOf($scope.search) != -1 || item.gsx$response.$t.indexOf($scope.search) != -1) {
return true;
}
return false;
};
$scope.highlightText = function(text, search) {
if (search && search.length === 0) {
// Returns the default content.
return $sce.trustAsHtml(text);
}
// Define a regular expression to find the text globally and ignoring capital letters.
var regex = new RegExp(search, 'gi');
// If you already found the text then inject a span element with CSS class to highlight that you found.
return $sce.trustAsHtml(text.replace(regex, '<span class="foundText">$&</span>'));
};
}]);
.foundText {
background-color: #ff0;
color: #f00;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular.min.js"></script>
<div ng-app="sample" ng-controller="sampleController">
<div class="black">
<input type="text" name="search" ng-model="search" placeholder="search" ng-click="didSelectLanguage()" />
</div>
<br>
<br>
<br>
<table style="border: 1px solid black ;">
<tbody>
<tr>
<td>
<center><b>Question</b></center>
</td>
<td>
<center><b>Response</b></center>
</td>
</tr>
<tr ng-repeat="user in users | filter:searchFilter">
<td style="border: 1px solid black ; width:30%;white-space: pre-wrap;" ng-bind-html="highlightText(user.gsx$topic.$t, search)">{{user.gsx$topic.$t}}</td>
<td style="border: 1px solid black ; width:70%;white-space: pre-wrap;" ng-bind-html="highlightText(user.gsx$response.$t, search)">{{user.gsx$response.$t}}</td>
</tr>
</tbody>
</table>
</div>
希望这会有所帮助!