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

前端 未结 8 727
自闭症患者
自闭症患者 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:42

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

    <p ng-bind-html="message  | limitTo: 150 | sanitize"></p>
    
    0 讨论(0)
  • 2021-01-13 02:51

    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.

    0 讨论(0)
提交回复
热议问题