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

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

    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.

提交回复
热议问题