At some point, the provided AngularJS data filters will not be enough to fill your needs, and you will need to create your own data filters. For example, assume that in an application that you are building, you have a region of the page that is limited in physical dimensions, but contains an arbitrary amount of text. You would like to truncate that text to a length which is guaranteed to fit in the limited space. A custom filter, as you might imagine, is perfect for this task.
The filter you wish to build accepts a string argument and returns another string. For now, the filter will truncate the string to 100 characters and append an ellipsis at the point of truncation:
(app.js) angular.module('myApp', []) .filter('simpletruncate', function () { // the text parameter return function (text) { var truncated = text.slice(0, 100); if (text.length > 100) { truncated += '...'; } return truncated; }; });
This will be used in...