0

I'm using truncate.js https://github.com/sparkalow/angular-truncate and it works great for codes like this:

{{announcement.content | characters:25}}

However, i can't seem to setup for the following and i can't get it to work:

<p ng-bind-html="parseTrustedHtml(announcement.content | characters : 25)"></p>
3
  • can you provide a jsFiddle? Commented Sep 10, 2015 at 9:24
  • I'm not sure if i can. I couldn't install the angular.js on jsFiddle :( Commented Sep 10, 2015 at 9:38
  • Could you provide a parseTrustedHtml function? Try $filter('characters') in an controller function. Commented Sep 10, 2015 at 11:12

3 Answers 3

2

I encountered a similar issue, the issue is that angular-truncate is meant for strings, not HTML. Here is my solution:

Markup:

<div class="container" ng-controller="parentCtrl">
   <div ng-bind-html="text | limitHtml : maxNumberOfChar:'...' | trustAsHtml"></div>
</div>

Code:

    .filter('trustAsHtml', ['$sce', function($sce) {
        return $sce.trustAsHtml;
    }])
    .filter('limitHtml', function() {
        return function(text, limit, ellipsis) {
            var _getClosedTagsString = function(_tagArray) {
                var _returnArray = [],
                _getTagType = function(_string) {
                    return _string.replace(/<[\/]?([^>]*)>/,"$1");
                };

                angular.forEach(_tagArray,function(_tag,_i) {
                    if(/<\//.test(_tag)) {
                        if(_i === 0) {
                            _returnArray.push(_tag);
                        } else if(_getTagType(_tag) !== _getTagType(_tagArray[_i - 1])) {
                            _returnArray.push(_tag);
                        }
                    }
                });
                return _returnArray.join('');
            },
            _countNonHtmlCharToLimit = function(_text,_limit) {
                var _isMarkup = false,
                _isSpecialChar = false,
                _break = false,
                _underLimit = false,
                _totalText = 0,
                _totalChar = 0,
                _element,
                _return = {
                    textCounter   : 0,
                    offsetCounter : 0,
                    setEllipsis   : false,
                    overElementArray : []
                };
                angular.forEach(_text,function(_c) {
                    _underLimit = _return.textCounter < _limit;
                    if(_c === '<' && !_isMarkup && !_isSpecialChar) {
                        (!_underLimit) && (_element = '<');
                        _isMarkup = true;
                    } else if(_c === '&' && !_isMarkup && !_isSpecialChar) {
                        _isSpecialChar = true;
                    } else if(_isMarkup) {
                        //tracking html elements that are beyond the text limit
                        (!_underLimit) && (_element = _element + _c);
                        if(_c === '>') {
                            //push element in array if it is complete, and we are
                            //beyond text limit, to close any html that is unclosed
                            (!_underLimit) && (_return.overElementArray.push(_element));
                            _break = true;
                            _isMarkup = false;
                        }
                    } else if(_c === ';' && _isSpecialChar) {
                        _isSpecialChar = false;
                        //count as one character
                        _return.textCounter++;
                        _break = true;
                    }

                    if(_underLimit) {
                        if(!_isMarkup && !_isSpecialChar && !_break) {
                            //counting number of characters in non html string
                            _return.textCounter++;
                        }
                        _return.offsetCounter++;
                    } else {
                        _return.setEllipsis = true
                    }
                    _break = false;

                });

                //returns offset within html of number of non html characters found
                return _return;
            },
            _charToLimitOutput = _countNonHtmlCharToLimit(text.toString(),limit);

            return text.toString().substr(0, _charToLimitOutput.offsetCounter) +
                ellipsis + _getClosedTagsString(_charToLimitOutput.overElementArray);
        }
    })
    .controller('parentCtrl', function($scope,$timeout) {
        $scope.text = "<span><h1>Example&nbsp;</h1><p>Special Text</p><div>other stuff</div></span>";
        $scope.maxNumberOfChar = 10;
    });
Sign up to request clarification or add additional context in comments.

Comments

1

No need to use truncate.js You can solve this using custom directives and filters. try this one: https://stackoverflow.com/a/45076560/6816707

Comments

0

Need more information, however can you check if parseTrustedHtml method/function is available at $scope or $rootscope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.