angularjs - Trim string to a certain word count -
i have description in template
<p>{{data.description}}</p>
i want trim description word number, first 20 words. have seen many filters trim characters. causes last word break in cases.
you need split description string words, using spaces, count it:
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) { input = inputwords.slice(0, words).join(' ') + '\u2026'; } } return input; }; });
first check if parameter number, i'm checking if description longer what trim at, , trim rest. , in view:
{{data.description | words:250}}
Comments
Post a Comment