Angular show more button determined by line count, not letter count

I am looking for a solution in Angular or CSS that will automatically add a "read more" button if the text is longer than 5 lines. My Angular page currently limits text to 150 characters.

{{post.post_text | limitTo:letterLimit}}

However, some posts are too long because they contain multiple line breaks:

  • my post
  • line 2
  • line 3
  • l
  • i
  • n
  • e

While these posts are still under 150 characters, they begin to disrupt the layout of my page. I am seeking a solution that will add a "read more" button for text longer than 5 lines.

As a beginner in Angular, I am unsure where to start and would appreciate any assistance.

so far

I have only come across solutions based on character count, but I require a solution based on the number of lines or total line-height. Any help on this matter would be greatly appreciated. Thank you.

Answer №1

If you're looking to implement a filter, you might want to consider this approach:

{{post.post_content | textLimit:charLimit | displayLines:lineLimit}} // adjust line limit by toggling show more

app.filter('displayLines', function() {
    return function(text, limit) {

        var segments = text.split("\n");

        if(limit == 0) return segments.join('<br/>');
        return segments.slice(0,limit).join('<br/>');
    }
})

In your view:

<button class="show-more" ng-if="checkLines(checkLines(resource.body)"></button>

In your Controller:

$scope.line_limit_default = 4;// example value

function checkLines(text) {
  if(text && text != null) {   
    return (text.split("\n").length > $scope.line_limit_default) ? true : false;
  } else return false;
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with issues related to AngularJS auto-tab functionality

I implemented the code below to enable "auto tab" functionality with AngularJS, which automatically shifts focus to the "Title" textbox after the maximum length is reached in the "Name" textbox: var app = angular.module('plunker', []); app.dire ...

Trouble Loading TypeScript Class in Cast Situation

I've encountered an issue with my TypeScript model while using it in a cast. The model does not load properly when the application is running, preventing me from accessing any functions within it. Model export class DataIDElement extends HTMLElement ...

Properly implement changes in scope following the receipt of server-sent events

My software application receives frequent updates and usually functions well. However, I have noticed that in production environments where the volume of data is larger, my scopes do not always update correctly. Here is my current approach: var handleOrd ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Height of Parent Div minus Margin Top equal to 100%

I am working on a layout that requires three divs with different heights and header sizes to be positioned 100% from the top of their parent, while taking into account the height of the headers. I could use jQuery to achieve this, but since it's a res ...

Tips for fixing the position without affecting the width

I'm trying to figure out how to keep my #header fixed in position without it expanding in width. Despite checking my code multiple times, I can't seem to identify the factor causing my header to become wider and shift downwards unexpectedly. I in ...

Highlighting table column when input is selected

I am working with a table where each <td> contains an <input>. My goal is to apply the class .highlighted to all the column <td>s when an <input> is being focused on. Additionally, I want to remove this class from all other columns ...

Unusual shift in behavior - <input> on Windows 8.1

Our application is a unique HTML5/WinJS Google Talk app tailored for Windows 8 users. Within our app, we utilize a textarea for chat input with a predefined maximum height. We have coded the height to automatically adjust to the maximum limit once reached. ...

Navigate using Ui-Router or else utilize ng-admin

Currently, I am facing an issue in my angular application. I have integrated ng-admin to create a simple admin panel and also included Authentication features. My goal is to redirect the user to a login state when there is no active session. Upon integrat ...

Can a low-opacity gradient be merged with a solid background color?

Can anyone help with this code snippet? Here's the link table { height: 200px; width: 200px; background-color: #444557; /* seems to be hidden by the gradient */ /* Here is the CSS for a gradient background */ /* Source: http://colorzilla ...

Converting JSON into Typescript class within an Angular application

As I work on my app using angular and typescript, everything is coming together smoothly except for one persistent issue. I have entity/model classes that I want to pass around in the app, with data sourced from JSON through $resource calls. Here's ...

Steps for opening a clicked link in a new tab:

I have a link on my page that I would like to open in a new tab when clicked, but it doesn't seem to be working. Can anyone offer some suggestions or help? So far, I've tried the following code: <a target="_BLANK" ng-href="{{news.url ...

Concealing or revealing an image with jQuery when hovering

I currently have 3 <a> tags within my html, each containing 2 images. Specifically, the greyscale images are the ones that are visible while the colored ones are set to display:none. I am aiming to achieve a functionality where upon hovering over th ...

Ways to transfer JavaScript arguments to CSS style

Currently, I am developing a web service using Angular and Spring Boot. In this project, I have implemented cdkDrag functionality to allow users to place images in specific locations within the workspace. My goal is to maintain the placement of these image ...

Accessing the current clicked item in $scope using an Angular Directive within ng-repeat

I have set up a custom directive called calendar which includes a date picker created in JQuery. To associate the ng-model with the date picker, I am using the following code successfully: var clickedID = "#" + $scope.indexid + "_datePicker"; $(clickedID ...

Discover the method for creating internal links on a single page with AngularJS

What is the best way to create internal links using angular js? In html, we typically use <a href="#top">Top</a> to navigate to an element with the id "top". ...

Modifying KineticJs.Image with css styling

Hey there, I'm currently working on a project that involves canvas manipulation. I've successfully figured out how to draw an image and move it within the canvas, which wasn't too difficult to achieve. However, I'm now facing a challeng ...

Is it possible for me to position an element beside the element before it?

Imagine a traditional layout consisting of paragraphs grouped with a sidebar. However, there are two issues with this setup: first, the browser displays the sidebar (typically used for navigation) before the main content, causing problems for screen reader ...

The tooltip for the Google+ button stopped working

After setting up my Portfolio, I added a Google+ button. However, the page lacks styling and there seems to be an issue with the tooltip causing delays. Can anyone help me identify where the problem lies? ...

I'm curious about the significance of the "packages" property in the requireJs config. Can someone explain how it functions?

I'm currently trying to grasp the concept of requireJs and its functionality. In my exploration, I encountered a configuration property called "packages." From what I gather, 'packages' is intended for specifying a complete folder or module ...