Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Limit text length using angularJS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 573
    Comment on it

    Many times we run in a condition that the whole text is taking to much of space, so we need to show data upto certain limit and end it to ...(3 dots). 

    Usually we write code to check the length of the string and then trimming it, angular made it very simple. We have 2 ways to do it:

    1) Write this code directly in the place where we need to show the data.

    {{ myString | limitTo: 20 }}{{myString.length > 20 ? '...' : ''}}

    2) Using filters:

    angular.module('ng').filter('cut', function () {
            return function (value, wordwise, max, tail) {
                if (!value) return '';
    
                max = parseInt(max, 10);
                if (!max) return value;
                if (value.length <= max) return value;
    
                value = value.substr(0, max);
                if (wordwise) {
                    var lastspace = value.lastIndexOf(' ');
                    if (lastspace != -1) {
                      //Also remove . and , so its gives a cleaner result.
                      if (value.charAt(lastspace-1) == '.' || value.charAt(lastspace-1) == ',') {
                        lastspace = lastspace - 1;
                      }
                      value = value.substr(0, lastspace);
                    }
                }
    
                return value + (tail || ' ');
            };
        });

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: