Tôi đã làm điều này trong 1.x góc nhưng tôi muốn biết làm thế nào để làm nổi bật một từ động trong 2..in góc góc iam 1.x sử dụng một từ khóalàm thế nào để làm nổi bật một từ trong một đoạn trong góc 2?
<td><div highlight="var" keywords="somename"> {{paragraph}}</div></td>
Tôi đã làm html trên bởi sử dụng dưới đây angular-highlight.js.
angular.module('angular-highlight', [])
.directive('highlight', function()
{
var component = function(scope, element, attrs) {
if (!attrs.highlightClass) {
attrs.highlightClass = 'angular-highlight';
}
var replacer = function(match, item) {
return '<span class="'+attrs.highlightClass+'">'+match+'</span>';
}
var tokenize = function(keywords) {
keywords = keywords.replace(new RegExp(',$','g'), '').split(',');
var i;
var l = keywords.length;
for (i=0;i<l;i++) {
keywords[i] = '\\b'+keywords[i].replace(new RegExp('^ | $','g'), '')+'\\b';
}
return keywords;
}
scope.$watch('keywords', function() {
//console.log("scope.keywords",scope.keywords);
if (!scope.keywords || scope.keywords == '') {
element.html(scope.highlight);
return false;
}
var tokenized = tokenize(scope.keywords);
var regex = new RegExp(tokenized.join('|'), 'gmi');
//console.log("regex",regex);
// Find the words
var html = scope.highlight.replace(regex, replacer);
element.html(html);
});
}
return {
link: component,
replace: false,
scope: {
highlight: '=',
keywords: '='
}
};
});
được khuyến nghị bởi angular2 + để thao tác trực tiếp 'innerHTML'? –
Không trực tiếp, thông qua 'trình kết xuất' – Maxime