how to show long text with more button using angular?

前端 未结 5 830
执笔经年
执笔经年 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条回答
  • 2021-02-19 09:03

    I think there is an easier way.
    Just replace {{text}} with {{text | limitTo: 150}}, and then create below simple read more link.

    0 讨论(0)
  • 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: [
                '<div class="show-more-container"><ng-transclude></ng-transclude></div>',
                '<a href="#" class="show-more-expand">More</a>',
                '<a href="#" class="show-more-collapse">Less</a>',
            ].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;
    }
    <script src="https://code.angularjs.org/1.5.8/angular.js"></script>
    <div ng-app="app" ng-controller="TestController">
      <div show-more>
        All sorts of <strong>stuff</strong> can go in here.
        {{ loremIpsum }}
        <div>Even more divs</div>.
      </div>
      
      <div show-more>
        This <strong>won't</strong> truncate.
      </div>
    </div>

    0 讨论(0)
  • 2021-02-19 09:13

    angular-read-more
    https://github.com/ismarslomic/angular-read-more

    Demo
    http://codepen.io/ismarslomic/pen/yYMvrz

    <div hm-read-more
            hm-text="{{ text }}" 
            hm-limit="100" 
            hm-more-text="read more" 
            hm-less-text="read less"
            hm-dots-class="dots"
            hm-link-class="links">
    </div>
    
    0 讨论(0)
  • 2021-02-19 09:25

    Yes, this is totally possible with AngularJS -- but most of the solution is actually CSS.

    You'll be able to do this mostly through CSS. First, HTML/CSS doesn't really have a concept for how many lines a bunch of text is taking up. You can get the behavior you want by setting the height of a container element and the line-height of your text on the CSS line-height. For your default state, set the height based on two times your line height and set the overflow to hidden. Then you just need have your button conditionally apply a class that expands the height of the container and sets the overflow to visible:

    <style>
        .container {
             font-size: 16px;
             line-height: 16px;
             height: 32px;
             overflow: hidden;
        }
        .show {
             overflow: visible;
             height: auto;
        }
    <div class="container" ng-class="{show: show}">
        [your text]
    </div>
    <button ng-click="show = true">Show text</button>
    

    You can get fancy and make the button also hide the text again (as a toggle).

    0 讨论(0)
  • 2021-02-19 09:25

    ng-text-truncate
    https://github.com/lorenooliveira/ng-text-truncate

    Demo 1
    https://rawgit.com/lorenooliveira/ng-text-truncate/master/demo1.html

    Example
    <p ng-text-truncate="longTextVariable" ng-tt-chars-threshold="40"></p>

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