I'm currently working on a directive that should limit the text within an ng-repeat
loop to 50 characters and add an ellipses if it exceeds this length. Here is how my ng-repeat
is set up:
<li ng-repeat="result in topSuggestions.suggestions">
<h4>{{result.title}</h4>
<p suggestion-limiter>{{result.text}}</p>
</li>
This is the directive implementation:
app.directive("suggestionLimiter", [function () {
return {
restrict: "A",
link: function (scope, elem, attrs) {
console.log($(elem).text());
var maxTextLength = 50;
var ellipses = "...";
var currentText = $(elem).text();
if (currentText.length > maxTextLength) {
currentText = currentText.substr(0, maxTextLength - ellipses.length) + ellipses;
$(elem).text(currentText);
}
}
}
}]);
When I log $(elem).text()
, it displays as {{result.text}}
. I've attempted using if (scope.$last)
to ensure the completion of the ng-repeat
loop but still facing the same issue. Any suggestions on what might be causing this?