简体   繁体   English

如何根据字符数显示/隐藏div?

[英]How do you show/hide a div depending on character count?

I am trying to implement a show more/show less functionality in my div depending on number of characters. 我试图根据字符数在我的div中实现“多展示/少显示”功能。 Any ideas on how to do it using AngularJS? 关于如何使用AngularJS的任何想法? My div looks like follows: 我的div如下所示:

<section class="notes" ng-if="MyController.notes">
<p ng-bind="MyController.notes" ng-class="{'showMore': more, 'showLess': !more}"></p>
<section class="readMore" ng-click="OtherController.toggleMoreText($event)">
      <i class="icon-caret-down"></i>
      <span ng-click="more = !more">{{more ? 'Less' : 'More'}}</span>
</section>

If I want to show the "More" text only if the number of chars in the paragraph exceeds 250, how do I go about doing that? 如果仅当段落中的字符数超过250个时才显示“更多”文本,我该怎么做?

From the code I understand you want to use it in some item description or article? 从代码中我了解到您想在某些项目描述或文章中使用它? Here is one solution: 这是一种解决方案:

 var myApp = angular.module('myApp',[]); angular.module('myApp').controller('MyController',['$scope',function($scope){ $scope.notes = '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.'; $scope.showAll = false; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="MyController"> <section class="notes"> <p ng-class="{'show-full': showAll}" class="text">{{(!showAll)?notes.substring(0,150):notes}}</p> <section class="readMore"> <i class="icon-caret-down"></i> <a href="" ng-click="showAll = !showAll">{{showAll ? 'Less' : 'More'}}</a> </section> </div> </div> 

Its far from perfect but I haven't got time for anything better at the moment 它远非完美,但目前我还没有时间做任何更好的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM