Angularjs how to control the length of a string beyond a specified length in the ng-repeat process to display with ellipses

Source: Internet
Author: User

There is an array of data that needs to be displayed on the HTML page, which is now displayed in Ng-repeat mode. But the character length of the title in the array is longer, so if you want to limit the number of characters that are displayed on an HTML page, the content after the specified length is displayed as ellipses. How to achieve it?

HTML page:

<div ng-repeat="x in TU"> <img src="{{x.imgurl}}"> <h1>{{x.title}}</h1> <p>{{x.cost}}</p></div>

The data format is as follows:

$scope. TU = [{"Tuid":"Xy0001","Imgurl":"img/178.jpg", "title": "hahaha haha haha haha", "cost": "The" "" "}, { " tuid ":" xy0002 ", " Imgurl ":img/178.jpg", "title": Ha ha ha ha ha ha hehe , "Cost ": "" "},{" tuid ":" xy0003 ", " Imgurl": "Img/178.jpg", "title": "Hey hehe Hey hei hei hei Hey Hey Hei hei hei hey Hey heh hei hei hehe ", "cost":"89"}]


Write a filter:

Angular.module (' ng '). Filter (' Cut ',function() {Returnfunction(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 | | ' ... '); };});

How to use:

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

Parameters:

Tangent word (Bollinger bands)-If True, only the single word is cut.
Length (integer)-the maximum number of characters to retain.
After the 輟 (string, default: ' ... ')-next to the word.

or write it directly with someone else: angular-truncate Demo

Official API Https://docs.angularjs.org/api/ng/filter/limitTo

In the example HTML template:

Output numbers: {{numbers | limitto:numlimit}}

Angularjs how to control the length of a string beyond a specified length in the ng-repeat process to display with ellipses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.