0
간단한 표가 있으며 "추가"버튼을 클릭하면 다음 행이 추가됩니다.angularjs 필터를 사용하여 검색 결과 강조 표시
검색 입력 필드와 표 입력 필드 간의 일치를 강조 표시해야합니다.
오류와 함께이를 달성하기 위해 highlight
필터를 사용하려고하지만, 그것은 실행 :
"TypeError: Cannot read property 'replace' of undefined"
내가 그것을 어떻게 해결할 수 있을까? 아래 예제 코드 : ng-bind-html="x.text | highlight:search.text"
:
var app = angular.module("myApp",[]);
app.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)
}
});
app.controller("myCtrl", ['$scope', 'highlightFilter', function($scope, highlightFilter){
$scope.arr = [];
$scope.append = function(){
var x = {};
x.data1 = "";
x.data2 = "";
$scope.arr.push(x);
};
}]);
<!DOCTYPE html>
<html>
<head>
<title>Author's List</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<style>
.highlighted { background: yellow }
</style>
</head>
<body ng-controller="myCtrl" ng-app="myApp">
<div class="container">
<div class="btn-group">
<button ng-click ="append()" type="button" class="btn btn-default">Append</button>
<input type="text" placeholder="Search" ng-model="search.text">
<ul>
<div ng-repeat="x in arr | filter:search.text" ng-bind-html="x.text | highlight:search.text"></div>
</ul>
</div>
<form name ="myForm" novalidate>
<table class="table table-bordered">
<tr>
<th>data1</th>
<th>data2</th>
</tr>
<tr ng-repeat="x in arr">
<td><input ng-model="x.data1" required type="text" class="form-control"></td>
<td><input ng-model="x.data2" required type="text" class="form-control"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
감사합니다! 직접 입력에서 강조 할 수있는 모든 가능성? –
@VladyslavPlakhuta no, 왜냐하면'input'은 평문만을 지원하기 때문입니다. 유일한 것은'input'을'div'와'contenteditable = "true"'로 에뮬레이트하는 것입니다. 여기에서 예제를 볼 수 있습니다. https://stackoverflow.com/a/44675864/4222181 –