Trim string to a certain word count
问题 I have a description in my template <p>{{data.description}}</p> I want to trim this description to certain word number, like to first 20 words. I have seen many filters but they trim to a certain characters. This causes the last word to break in most cases. 回答1: You need to split the description string into words, using spaces, then count it: app.filter('words', function () { return function (input, words) { if (isNaN(words)) { return input; } if (words <= 0) { return ''; } if (input) { var