2

I'm building an app with angularJS, i have a $scope variable called description and it is basically just a long description of something in my database.

Is there a simple way to limit the number of characters that is outputted when i do {{description}}?

Or can it only be done through a separate $scope variable?

I would ideally want to limit the number of characters outputted by this $scope variable to 100.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Adam Cole
  • 173
  • 1
  • 3
  • 13
  • Possible duplicate of [How to slice/trim a $scope variable in AngularJS](https://stackoverflow.com/questions/37552570/how-to-slice-trim-a-scope-variable-in-angularjs) – Banzay May 11 '19 at 17:46
  • The proposed duplicate does not apply because the question asks how to do it without creating a separate variable. - [From review](https://stackoverflow.com/review/close/22986900). – georgeawg May 11 '19 at 18:59

1 Answers1

3

AngularJS has a built-in limitTo filter. In your case, it outputs a string limited to the specified number of characters. You can use it as follows:

<span>{{description | limitTo:100}}</span>

If you wish to show ellipsis when the string is trimmed, you can do something like that:

<span>{{description | limitTo:100}}{{description.length > 100 ? '&#8230;' : ''}}</span>

But, of course, nobody prevents you from doing all this job in your controller. Here I've presented more angular-ish way.

arcquim
  • 1,060
  • 1
  • 14
  • 24