Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
36 lines (25 loc) · 952 Bytes

File metadata and controls

36 lines (25 loc) · 952 Bytes
Copy raw file
Download raw file
Outline
Edit and raw actions

angularjs 字符串截取 filter,用于截取字符串

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) {
                    value = value.substr(0, lastspace);
                }
            }

            return value + (tail || ' …');
        };
    });

Usage:

{{some_text | cut:true:100:' ...'}}

Options:

  • wordwise (boolean) - if true, cut only by words bounds,

  • max (integer) - max length of the text, cut to this number of chars,

  • tail (string, default: ' …') - add this string to the input string if the string was cut.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.