Search code examples
angularjsangular-filters

AngularJS remove last character if it's a space (directly in HTML)


AngularJS does some magic straight inside HTML, I could say.

I am trying to achieve "stripped description" directly in HTML. So far I managed to limit the displayed characters and added triple dots.

<div ng-init="string = 'My string is cool'">
    {{ string | limitTo: 10 }}<span ng-show="string.length > 10">...</span>
</div>

But if the string (after limitation) ends with a space, I end up having "end ..." instead of "end...". Is there a way of removing that last space directly in HTML?


Solution

  • Just trim the limited string:

    <div ng-init="string = 'My string is cool'">
        {{( string | limitTo: 10 ).trim()}}<span ng-show="string.length > 10">...</span>
    </div>