I'm binding an html string to view using ngBindHtml. the string contents are plain text along with anchor elements.
for a plain text with ngBind I can use a filter like this one to limit text length to a certain word count.
I've tried it by creating a temp element on document and traversing along it's child nodes and counting the length of textContent:
app.filter('sliceTextContent', function () {
return function (str, max) {
var counter = 0;
var shortenedStr = '';
var wrap = document.createElement('div');
wrap.innerHTML = str;
for (var i = 0; i < wrap.childNodes.length; i++) {
if (wrap.childNodes[i].textContent.length + counter < max) {
shortenedStr += (wrap.childNodes[i].innerHTML) ? wrap.childNodes[i].innerHTML : wrap.childNodes[i].textContent;
counter += wrap.childNodes[i].textContent.length;
} else {
wrap.childNodes[i].textContent = wrap.childNodes[i].textContent.substr(0, max - counter);
shortenedStr += (wrap.childNodes[i].innerHTML) ? wrap.childNodes[i].innerHTML : wrap.childNodes[i].textContent;
break;
}
};
return shortenedStr;
}
});
I think this is not optimal and may cause timing issues while encountering long strings which I have in my database. Do you have ideas to improve it?
<a>tag but not all of it