8

I am trying to limit the characters i see on my angular js app. Currently i am using:

<p ng-bind-html="item.get('Content') | limitTo: 150"></p>  

But no joy, any ideas...

Sole
  • 3,100
  • 14
  • 58
  • 112

8 Answers8

6

I don't think that will work with ng-bind-html. This is for binding actual html code to the page. ng-bind should work fine.

<p ng-bind="item.get('Content') | limitTo: 150"></p>  

See plunkr http://plnkr.co/edit/y0LXMMFi6sU9AhShvuha?p=preview

EDIT:

Since you do have HTML code in it, you'll need to use ngSanitize. You can read about that here: https://docs.angularjs.org/api/ngSanitize

Add the reference to angular-sanitize.js, then import it into the app

var app = angular.module('plunker', ['ngSanitize']);

Then your original code should work fine, although it's likely parts of it will be cut off, including ending tags, so you'll need to deal with that.

See plnkr: http://plnkr.co/edit/y0LXMMFi6sU9AhShvuha?p=preview

Timothy
  • 1,198
  • 3
  • 10
  • 30
  • 1
    You are assuming the op is using ng-bind-html for no reason... why? – aw04 Jul 15 '15 at 14:38
  • He wants to limit the characters on the page... that hints pure text to me. If he wants to add divs and such with the code he'll have to use a totally different approach.
    adds 5 characters plus another 6 for
    . If I'm wrong, then this isn't the correct answer, but only the OP knows that for certain.
    – Timothy Jul 15 '15 at 14:40
  • Hey Guys, the data is being brought in from Parse, which has html tags in their... so that is why I am using ng-bind-html – Sole Jul 15 '15 at 14:41
  • See my edit, unless you're already doing that and only want to limit the characters shown, which as I type this, I assume is the scenario.... Is that correct? – Timothy Jul 15 '15 at 14:48
  • Hi Timothy, I am already using nGSanitize and the original code i pasted was not working.... – Sole Jul 15 '15 at 15:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83349/discussion-between-sole-and-timothy). – Sole Jul 15 '15 at 15:36
  • What about showing the first **paragraph** only? How would you do that? – Razvan Zamfir Aug 25 '19 at 04:55
3

since you use ng-bind-html you also need $sce, so my advice do it in your controller. Like so

Ctrl.$inject= ['$sce', '$filter', '$scope'];
Function Ctrl($sce, $filter, $scope) {
  $scope.content= $filter('limitTo')(dataWithHtml, 100, 0);
  $scope.content= $sce.trustAsHtml($scope.content);
}

on html you can use like so:

<p ng-bind-html="content"></p>

in this case I assume your original data is dataWithHtml, 100 is the limit number, 0 is the starting point. More details please refer to official documentations.

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
3

integrate this link :

 <script src="https://code.angularjs.org/1.2.16/angular-sanitize.js"></script>

check if you have sanitize here

var adminApp = angular.module('adminApp', ['ngSanitize', ...,])

in your html you macke this code :

<p ng-bind-html="item.get('Content') | limitTo: 150"></td>
Ait Friha Zaid
  • 1,222
  • 1
  • 13
  • 20
0

In your controller

$scope.contentText = item.get('Content');

In your html

<p>{{ contentText | limitTo: 150 }} </p>

ODelibalta
  • 2,194
  • 1
  • 18
  • 28
0

Inject $sce into your controller and then use it like below:

$scope.contentText = $sce.trustAsHtml(item.get('Content')); 

In your html

<p ng-bind-html="contentText | limitTo: 150"></p>
Axe
  • 6,285
  • 3
  • 31
  • 38
0

You can use this without function if you are using sanitize filter

<p ng-bind-html="message  | limitTo: 150 | sanitize"></p>
Sandeep Sherpur
  • 2,418
  • 25
  • 27
0

If none of the answers above didn't satisfy you, how about the following approach?

var app = angular.module('myTestNgApp', ['ngSanitize']);

app.controller('myMainTestCtrl', function($scope) {
  $scope.longText = "This is a very loooooooooooooooooooong text";
})
.filter('textShortenerFilter', function() {
  return function(text, length) {
    if (text.length > length) {
      return text.substr(0, length) + "...";
    }
    return text;
  }
});

Working example:- https://jsfiddle.net/anjanasilva/8xk9eL0t/

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
0

You can use custom directive and filters which will work same as ng-bind-html

<p to-html>{{content | limitTo:200}}</P>

directive:

.directive('toHtml', function($timeout,$sce) {
   return {
          restrict: 'A',
          link: function($scope, element, attrs, ctrl) {
             $timeout(function() {
                element.html($sce.getTrustedHtml(element[0].innerText) || '');
             }, 200);
          }
         };
})

Truncate with ellipsis:

<p to-html>{{content | limitWithEllipsis:200}}</P>

you need to use custom filter(limitWithEllipsis) with above directive

.filter('limitWithEllipsis', function() {
          return function(input, limit, begin) {
              var str='';
                if (Math.abs(Number(limit)) === Infinity) {
                  limit = Number(limit);
                } else {
                  limit = parseInt(limit);
                }
                if (isNaN(limit)) return input;

                if (angular.isNumber(input)) input = input.toString();
                if (!angular.isArray(input) && !angular.isString(input)) return input;
                if(input.length<=limit) return input;
                begin = (!begin || isNaN(begin)) ? 0 : parseInt(begin);
                begin = (begin < 0) ? Math.max(0, input.length + begin) : begin;

                if (limit >= 0) {
                  str=input.slice(begin, begin + limit);
                  return str.concat('....'); 
                } else {
                  if (begin === 0) {
                     str=input.slice(limit, input.length);
                    return str.concat('....');
                  } else {
                     str=input.slice(Math.max(0, begin + limit), begin);
                    return str.concat('....');
                  }
                }
            };
        })