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...

0

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

7 Comments

You are assuming the op is using ng-bind-html for no reason... why?
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. <div> adds 5 characters plus another 6 for </div>. If I'm wrong, then this isn't the correct answer, but only the OP knows that for certain.
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
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?
Hi Timothy, I am already using nGSanitize and the original code i pasted was not working....
|
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.

Comments

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>

Comments

0

In your controller

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

In your html

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

1 Comment

This doesn't use ng-bind-html
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>

Comments

0

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

<p ng-bind-html="message  | limitTo: 150 | sanitize"></p>

Comments

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/

Comments

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('....');
                  }
                }
            };
        })

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.