var myApp = angular.module('myApp', []); myApp.filter('truncate', [function () { return function (text, length, end) { if (isNaN(length)) { length = 10; } if (end === void 0) { end = '...'; } if (text.length <= length) { return text; } return text.substring(0, length - end.length) + end; }; }]);
<div ng-app="myApp"> <p>{{ '1234567890' | truncate }}</p> <p>{{ '12345678901' | truncate }}</p> <p>{{ '1234567890' | truncate:4 }}</p> <p>{{ '1234567890' | truncate:3 }}</p> <p>{{ '1234567890' | truncate:2 }}</p> <p>{{ '12345678901' | truncate:10:'---' }}</p> </div>