I am working on displaying a String text and determining how many words to hide based on the screen width.
Here is my current approach:
app.filter('words', function () {
return function (input, words) {
if (isNaN(words)) return input;
if (words <= 0) return '';
if (input) {
var inputWords = input.split(/\s+/);
if (inputWords.length > words) {
var wordCount = inputWords.length - words;
input = inputWords.slice(0, words).join(' ') + ' + ' + wordCount;
}
}
return input;
};
});
This filter currently works for a fixed count of words. For instance, if words
is set to 5, only 5 words will be displayed while the rest are hidden.
However, I am looking for a way to make the number of words
dynamic based on the width of the element. For example, if a <div>
has a width of 200px, I want to show 12 words, adjusting the count based on the width.
I believe a directive that can take the element's width and calculate the appropriate number of words
is needed.
Check out this Demo:
Thank you in advance for your assistance.