I'm trying to write a set of filters to highlight and then dehighlight dynamically generated html:
Highlight filter:
app.filter('highlight', function ($sce) {
return function (str, termsToHighlight) {
// Sort terms by length
termsToHighlight.sort(function (a, b) {
return b.length - a.length;
});
var regex = new RegExp('(' + termsToHighlight.join('|') + ')', 'g');
return $sce.trustAsHtml(str.toString().replace(regex, '<span class="highlightedSpan" style="background-color: yellow; ' +
'font-weight: bold;">$&</span>'));
};
});
Dehighlight:
app.filter('dehighlight', function ($sce) {
return function (str) {
var obj = $($.parseHTML(str));
obj = obj.find('.highlightedSpan').replaceWith(function () { return this.innerHTML; });
return $sce.trustAsHtml(obj.html());
};
});
I'm trying to strip away the span tag and leave the original text, but I'm not sure if it's working or what to return. Str is the html string. Any help would be greatly appreciated!
Attribute binding in Angular helps you set values for attributes directly. With attribute binding, you can improve accessibility, style your application dynamically, and manage multiple CSS classes or styles simultaneously.
The three types of directives in Angular are attribute directives, structural directives, and components.
The attribute directive changes the appearance or behavior of a DOM element. These directives look like regular HTML attributes in templates. The ngModel directive which is used for two-way is an example of an attribute directive.
We found marking terms with Angular to be a non-trivial task and ended up using an outside library (Mark.js). Manipulating the html directly like that can break angular's scoping and create other issues. If each possible term is in it's own little html tag, then you can use a filter on each one to test against a shared list of terms (either passed in or as part of a service), just remember that the "filter" is finicky about what changing inputs will fire a dirty-check and re-evaluate.
Just for completeness sake, I got it in the end, the find function was misbehaving from my POV.
Final solution for dehighlighting:
app.filter('dehighlight', function () {
return function (str) {
var obj = $('<div/>').append(str);
obj.find('span.highlightedSpan').replaceWith(function () { return this.innerHTML; });
return obj.html().toString();
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With