最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show more functionality in AngularJS? - Stack Overflow

programmeradmin1浏览0评论

I have a list of items along with their information. The problem is that I want to show the description up to 50 characters. If this value is exceeded I want to show a show more button. When that is clicked, I want to show the full text. I want to do it with filters but I don't know how to achieve this.

{{jobs.description | limitTo: 2}}{{jobs.description.length>20 ? '...':''}}

Is there a possibility I can write <a href="">show more</a> link at the location of the characters ...?

Or is there another method of achieving my goal?

I have a list of items along with their information. The problem is that I want to show the description up to 50 characters. If this value is exceeded I want to show a show more button. When that is clicked, I want to show the full text. I want to do it with filters but I don't know how to achieve this.

{{jobs.description | limitTo: 2}}{{jobs.description.length>20 ? '...':''}}

Is there a possibility I can write <a href="">show more</a> link at the location of the characters ...?

Or is there another method of achieving my goal?

Share Improve this question edited Apr 16, 2017 at 18:47 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Apr 7, 2017 at 16:49 Usman IqbalUsman Iqbal 2,4296 gold badges32 silver badges52 bronze badges 3
  • did you check my answer ? – Rohìt Jíndal Commented Apr 9, 2017 at 8:28
  • Rohit please run the snippet – Usman Iqbal Commented Apr 10, 2017 at 6:17
  • Thanks for correcting me.I updated my answer snippet. can you please check now. – Rohìt Jíndal Commented Apr 10, 2017 at 6:56
Add a ment  | 

2 Answers 2

Reset to default 6

Observation:

  • Your implementation is correct. The issue is with your AngularJS version.
  • The AngularJS limitTo filter is available for both Array and Strings from v1.2.1 onwards.

Working Demo

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {

    // Initial 50 characters will be displayed.
    $scope.strLimit = 50;

    // String
    $scope.jobs = {
      description: "Hi I have a list of items along with their information. The problem is I want to show the description up to 50 letters, but if it exceeds this value I want to show show more button upon clicking it I want to show the full text. I want to do it with filters, but I don't know one could achieve this with my way."
    };

  // Event trigger on click of the Show more button.
   $scope.showMore = function() {
     $scope.strLimit = $scope.jobs.description.length;
   };
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  {{ jobs.description | limitTo:strLimit }}
  <span ng-if="jobs.description.length > 50">
    <button ng-click="showMore()">Show more</button>
  </span>
</div>

Updated Plnkr as per the ment with show less functionality.

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {

    // Initial 50 characters will be displayed.
    $scope.strLimit = 50;

    // String
    $scope.jobs = {
      description: "Hi. I have a list of items along with their information. The problem is I want to show the description up to 50 letters, but if it exceeds this value I want to show show more button upon clicking it I want to show the full text. I want to do it with filters, but I don't know one could achieve this with my way."
    };

  // Event trigger on click of the Show more button.
   $scope.showMore = function() {
     $scope.strLimit = $scope.jobs.description.length;
   };

  // Event trigger on click on the Show less button.
   $scope.showLess = function() {
     $scope.strLimit = 50;
   };
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  {{ jobs.description | limitTo:strLimit }}
  <span ng-if="jobs.description.length > 50 && jobs.description.length != strLimit">
    <button ng-click="showMore()">Show more</button>
  </span>
  <span ng-if="jobs.description.length == strLimit">
    <button ng-click="showLess()">Show less</button>
  </span>
</div>

You don't need any directives to achieve this.

Simply refer to plnkr.co/edit/G3XxBnvAKhc53qy4foPr?p=preview; I just created a sample. The limitTo filter is more than enough to achieve.

发布评论

评论列表(0)

  1. 暂无评论