3

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?

3
  • did you check my answer ? Commented Apr 9, 2017 at 8:28
  • Rohit please run the snippet Commented Apr 10, 2017 at 6:17
  • Thanks for correcting me.I updated my answer snippet. can you please check now. Commented Apr 10, 2017 at 6:56

2 Answers 2

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.com/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 comment 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.com/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>

Sign up to request clarification or add additional context in comments.

6 Comments

Is everybody seeing these script errors in the snippet console or its just me ?
in your question you write about show more only .hence, show less functionality was not implemented. If you want i can write show less functionality as well.
@UsmanIqbal I update the answer as per the Show less functionality requirement.
@rohit although i have implemented my way and it worked but still i will mark your answer correct. Cheers
Let's say if I default everything to show less. Is there a way to make the first instance in the list to show more?
|
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.