Simple way to limit characters in ng-bind-html Angular JS

前端 未结 8 816
自闭症患者
自闭症患者 2021-01-13 02:15

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 02:39

    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/

提交回复
热议问题