how to show long text with more button using angular?

前端 未结 5 840
执笔经年
执笔经年 2021-02-19 08:22

i have long text like that :-

\"5 Simple Steps to Improve Patient Experience5 Simple Steps to Improve Patient Experience5 Simple Steps to Improve Patient Experience5 Si

5条回答
  •  萌比男神i
    2021-02-19 09:09

    If you'd prefer to have a div that truncates itself based on pixel height instead of character count, you can try this. This allows you to put nested HTML in your expandable section.

    angular.module('app', [])
    .controller('TestController', function($scope) {
      $scope.loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
    })
    .directive('showMore', function() {
      return {
            restrict: 'A',
            transclude: true,
            template: [
                '
    ', 'More', 'Less', ].join(''), link: function(scope, element, attrs, controller) { var maxHeight = 45; var initialized = null; var containerDom = element.children()[0]; var $showMore = angular.element(element.children()[1]); var $showLess = angular.element(element.children()[2]); scope.$watch(function () { // Watch for any change in the innerHTML. The container may start off empty or small, // and then grow as data is added. return containerDom.innerHTML; }, function () { if (null !== initialized) { // This collapse has already been initialized. return; } if (containerDom.clientHeight <= maxHeight) { // Don't initialize collapse unless the content container is too tall. return; } $showMore.on('click', function () { element.removeClass('show-more-collapsed'); element.addClass('show-more-expanded'); containerDom.style.height = null; }); $showLess.on('click', function () { element.removeClass('show-more-expanded'); element.addClass('show-more-collapsed'); containerDom.style.height = maxHeight + 'px'; }); initialized = true; $showLess.triggerHandler('click'); }); }, }; });
    .show-more-container {
        overflow: hidden;
    }
    
    .show-more-collapse, .show-more-expand {
        text-align: center;
        display: none;
    }
    
    .show-more-expanded > .show-more-collapse {
        display: inherit;
    }
    
    .show-more-collapsed > .show-more-expand {
        display: inherit;
    }
    
    
    All sorts of stuff can go in here. {{ loremIpsum }}
    Even more divs
    .
    This won't truncate.

提交回复
热议问题