Truncate multi-line HTML bound with show less /more option

I have a similar issue to this question. The problem with this solution is that jquery.autoellipsis slows down my app and also needs to be changed in order to add the show more/less functionality.

In my case, I'm getting dynamic content as html code from ng-bind-html in ng-repeat.

  <ion-item ng-repeat="page in pages">
     <div ng-bind-html="page.extract" class="item item-text-wrap"></div>
  </ion-item>

I would like to have a directive that truncates html code after X pixels (Height) or Y lines, and also shows the option view more/less in case there is content to show.

Most of the solutions that I found are just for plain text or require other Plugins such as More.

Thanks.

After testing some of the Jquery plugins available, this is the one that work bests: Trunk8

Reasons:

  1. Load faster.
  2. Works with plain test and html (not so much code).
  3. Costumization can be done in Directive.

Steps:

  1. Download Trunk8
  2. Add access to jQuery and the Plugin: trunk8.js
  3. Create Directive (see below)
  4. Add access to Directive (if necessary)
  5. Access to modude from app.js: var app = angular.module('app', ['ellipsis'])
  6. Call Directive from your view.

Directive:

angular.module( 'ellipsis', [])
    .directive('ellipsis', [function () {
    return {
        required: 'ngBindHtml',
        restrict: 'A',
        priority: 100,
        link: function ($scope, element, attrs, ctrl) {
            $scope.hasEllipsis = false;
            $scope.$watch(element.html(), function(value) {
               if (!$scope.hasEllipsis) {
                   // apply this code ONCE
                   $scope.hasEllipsis = true;
                   $(element).trunk8({
                        fill: '&hellip; <a id="read-more" href="#">read more</a>', /*(Default: '&hellip;') The string to insert in place of the omitted text. This value may include HTML.*/
                        lines: 3, /*(Default: 1) The number of lines of text-wrap to tolerate before truncating. This value must be an integer greater than or equal to 1.*/
                        //side: 'right', /*(Default: 'right') The side of the text from which to truncate. Valid values include 'center', 'left', and 'right'.*/
                        tooltip: false, /*(Default: true) When true, the title attribute of the targeted HTML element will be set to the original, untruncated string. Valid values include true and false.*/
                        //width: 'auto', /*(Default: 'auto') The width, in characters, of the desired text. When set to 'auto', trunk8 will maximize the amount of text without spilling over.*/
                        parseHTML: true /*(Default: 'false') When true, parse and save html structure and restore structure in the truncated text.*/
                        //onTruncate /*(Callback): Called after truncation is completed.*/
                   });
                   $(element).on('click', '#read-more', function (event) {
                        $(element).trunk8('revert').append(' <a id="read-less" href="#">read less</a>');
                   });
                   $(element).on('click', '#read-less', function (event) {
                        $(element).trunk8();
                   });
                }
            });
        }
    };
}]);

View:

<ion-item ng-repeat="page in pages">
  <div ng-bind-html="page.extract" class="item item-text-wrap" ellipsis></div>
</ion-item>

First result when I googlged "angular elipsis directive" - did you try that?

https://github.com/dibari/angular-ellipsis