Conditional more link for multi-line ellipsis in Angular

Looking for a multi-line ellipsis angular directive that will only show the More/Less link if the text exceeds a certain number of lines.

Here is the directive I have:

 angular.module('app')
        .directive('expandableDivContent', expandableDivContent);

    expandableDivContent.$inject = ['$timeout', 'jQuery'];

    function expandableDivContent($timeout, $) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs, ngFormCtrl) {

                scope.expandContent = function () {
                    var $box = $(element).find('.expandablecontent');
                    var $header = $(element).find('.expand');

                    var minimumHeight = 50;
                    // get current height
                    var currentHeight = $box.innerHeight();

                    // get height with auto applied
                    var autoHeight = $box.css('height', 'auto').innerHeight();

                    // reset height and revert to original if current and auto are equal
                    $box.css('height', currentHeight)
                        .animate({
                            height: (currentHeight === autoHeight ? minimumHeight : autoHeight)
                        });

                    var text = currentHeight === autoHeight ? '+ MORE' : '- LESS';
                    $header.text(text);

                }
            }
        }
    }

My CSS for this directive:

.expandablecontent {
    height: 50px;
    line-height: 25px;
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 10px;
}

With a line-height of 25px and max height set at 50px, it initially displays 2 lines without ellipsis "...."

This is my HTML:

<div expandable-div-content ng-if="step.comments">
                    <p class="expandablecontent">
                        <small>"{{step.comments}}"</small>
                    </p>
                    <div class="expand" ng-click="expandContent()" ng-if="step.comments.length > 50">
                         + More
                    </div>
                </div>

While this works well, I aim to display the more link only when the text exceeds 2 lines. My current condition based on character count fails when the container is resized to fit more characters into two lines.

How can I implement a scope variable in the directive to determine if the text exceeds a certain number of lines and efficiently display the More link across all modern browsers?

Answer №1

To achieve the desired effect, consider setting the line height to 18px/em and adjusting the height of the container div (where the text will be displayed) accordingly. You can then implement overflow-hidden with text-overflow: ellipsis to truncate the text if needed.

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

Adjust the dimensions of a Three.js-generated 3D Cylinder in real-time

Is it possible to dynamically change the dimensions of a 3D cylinder created using Three.js, similar to how we can adjust the size of a cube in this live example: http://jsfiddle.net/EtSf3/4/ Below is the code I have for the cylinder: HTML: <script s ...

Achieving Navbar overlap on Carousel with Bootstrap

Is there a way to make the bootstrap navbar overlap with the carousel without causing any issues with #test? HTML <nav class="navbar navbar-expand-lg navbar-dark bg-dark floating-navbar"> <div class="container"> & ...

Is there a comprehensive demo showcasing how to utilize Angular JS to interact with a REST/CRUD backend using various HTTP methods like GET, POST,

After reading through an article (link: here) outlining how to create a REST/CRUD backend, I successfully set up my own system using MongoDB locally instead of MongoLabs. Following Google's tutorial on utilizing ngResource and a Factory pattern, I ma ...

Prevent recursion in Angular UI Bootstrap Carousel

Currently implementing angular ui bootstrap carousel and trying to find a way to prevent the navigation recursion. Is there a reliable method to disable the back arrow when on the first slide or the right arrow when on the last slide? Note: Customizing th ...

Is there a way to trigger a click event on an href using asp code behind?

Is there a way to programmatically trigger a click event on the href "shipping_question_ID_product_received_as_ordered_link" from the code behind of an ASP form? This href triggers a complex function in jquery when clicked by the user. I need to simulate ...

A viewless Angular 2 component

Is it feasible to utilize Angular 2 without the need for a template or an @View? I am exploring alternative methods akin to the example shown below: Angular 1 index.html <div ng-controller="appcontroller"> <div ng-class="{active: isActive()}"& ...

When a user hovers over a sub-menu item in Bootstrap that has its own sub-menu, it causes the parent

I am facing an issue with my bootstrap drop-down menu. The main drop-down list has 3 items, each of which has a sub-list displayed on hover. However, when I hover over these items, the layout gets rearranged instead of maintaining its static appearance and ...

Issues with the functionality of the sliding menu in Angular are being encountered when trying to use $

Challenge I am facing an issue with a slider menu feature that I integrated into a mobile application for navigation purposes. The menu is functioning properly - it displays, allows flicking of the initial links, and can be closed by pushing the backdrop. ...

extract the ID from an array and switch its state

I'm currently working on developing an "auto toggle" feature. My initial idea was to use a loop for this purpose. The array[i].onclick part is functioning as expected, but I am facing challenges in implementing the toggle animation. function setConte ...

Leveraging React useEffect for retrieving data and managing component behavior

Having trouble querying data, setting it to state with useEffect, and rendering the results? I've tried a few things but only see changes when state is updated. Any advice would be greatly appreciated. Thank you in advance. useEffect // fetchCamp ...

Update a div's content with a click

I'm looking for a way to update the data in my div whenever I click on a link that reveals the div. Despite finding many similar answers, none have worked for me. Here's what I have: I have multiple divs that become visible when clicking on lin ...

set available date with angular bootstrap datetimepicker

Currently, I am utilizing an angular bootstrap datetimepicker plugin that can be found at https://github.com/dalelotts/angular-bootstrap-datetimepicker. My goal is to configure the calendar so that only dates within the range of today and seven days in t ...

Trouble with Bootstrap 4 Carousel functionality

Having encountered an issue with the carousel module on Bootstrap 4, I've spent a significant amount of time troubleshooting with no success. In the hopes of finding a solution, I'm reaching out here for assistance! (Despite checking other relat ...

Navigating different views in Angular with various layouts

Is there a way to implement different layouts for my backend? At the moment, I have three layouts - one for admin, one for user, and one for teacher, which are named adminLayout.html, userlayout.html, and teacherLayout.html for dashboards. This is how I ...

Tips for identifying when a video's autoplay feature does not function properly on all web browsers

My search for the most optimal method to automatically play a video in the background led me to discover some interesting findings based on the latest data available. VVC/x266 - Versatile Video Coding: Offers significant reduction in data requirements wi ...

Picture emerges from the lineup

My dilemma involves repositioning an image within a row > col-md-6. To address this, I devised two classes - .pos1 and .pos2 .pos1 { position: relative; } .pos2 { position: absolute; right: 0px; } However, when applying these classes, the ...

A third-party script is causing disruption to my JavaScript code

My website has a 3rd party script that displays certain data, but when this script loads, it causes all of the JavaScript on any page it's included in to break. However, removing the script resolves the issue and my page functions properly. Is there ...

Troubleshooting: Difficulty Injecting Chunks into Custom Template Using Webpack and HtmlWebpackPlugin

Utilizing Webpack/HtmlWebpackPlugin for compiling an AngularJS application with multiple entry points and chunks. The JavaScript needs to be injected into the HTML template at a specific location using: <% for (var chunk in htmlWebpackPlugin.files.chun ...

Issues with the Bootstrap Datetimepicker span breaking animation

I'm experiencing issues with the Bootstrap 3 Datepicker v4 as something seems to be breaking and I can't seem to fix it. My guess is that the input is causing a CSS issue. <div class="container"> <div class="row"> <div ...

When trying to use routes with the .map() function, it does not seem

I am currently working on developing a multi-page web application using react-router-dom. However, I have encountered an issue with the user list page (userlist.js) where the data inside the .map() function is not being returned. Interestingly, the code pr ...