實現關鍵字的高亮,主要是用到了將關鍵字動態的用em標簽包住,然后對em進行樣式操作。然后通過自定義filter,來完成對關鍵字的篩選。
<html> <head> <title></title> <meta charset='utf-8'/>/*js以及css的引用略*/ </head> <body ng-app = "myApp" controller = "myCtrl"> <ul> <li ng-repeat="log in logs">{{log | getKey:log.match}}</li> </ul> </body> </html>var myApp = angular.module('myApp',[]); myApp.controller('myCtrl',['$scope',function($scope){ $scope.logs =[ {'logName':'logOne','match':'one'}, {'logName':'logTwo','match':'two'}, {'logName':'logThree','match':'three'} ]; }]); myApp.filter('getKey',['$sce',function($sce){ return function(content,match) { var reg = new RegExp(match,'g'); content.replace(reg,'<em>'+match+'</em>'); return $sec.trustAsHtml(content); } }])