Looking at Mastering Web Development with AngularJS, I ran this custom filter to trim text, cut out the last 3 characters, and append ... if the input exceeds the LIMIT.
app.html
<div ng-controller="MyCtrl">
<table>
<thead>
<th>Name</th>
</thead>
<tbody>
<tr ng-repeat="s in strs | customTrim:2">
<td>{{s}}</td>
</tr>
</tbody>
</table>
app.js
var myApp = angular.module('myApp',[]);
myApp.controller("MyCtrl", function ($scope) {
$scope.strs = ["HEY THERE", "me"];
});
myApp.filter('customTrim', function($filter) {
var limitToFilter = $filter('limitTo');
console.log("limitToFilter('ABCDEF', 2)",
limitToFilter('ABCDEF', 2));
return function(input, limit) {
console.log("input:", input, "input.length", input.length,
"limit:", limit);
if(input.length > limit) {
console.log("returning...");
console.log(limitToFilter(input, limit-3) + "...");
return limitToFilter(input, limit-3) + "...";
}
return input;
}
});
However, the length of strs, rather than the individual s, appears to be printing out.
How can I pass each item of the array into my customFilter?
Console
limitToFilter('ABCDEF', 2) A
input: ["HEY THERE", "me"] input.length 2 limit: 2
input: ["HEY THERE", "me"] input.length 2 limit: 2