Displaying elements in Angular based on their height

I'm currently working on a paragraph that adjusts its length based on the maximum height. I'm interested in finding a way to display or hide the anchor element if the height exceeds a specific limit. Essentially, I only want to show "more..." when necessary.

$scope.hidden = true;
    .__Description {
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    }
    .less {
      max-height: 160px;
 
    }
<div class="__Description">
    <div class="__contents" ng-class="{less: hidden}">
    <div data-ng-bind-html="program.Description"></div>
    </div>
    </div>
    <a ng-click="hidden = !hidden">{{hidden? 'More...' : 'Less...'}}</a>

Answer №1

A possible solution could involve encapsulating the content and read more functionality within a directive.

The directive could monitor the height of the content, then trigger a scope property toggle.

Below is a basic outline in pseudo code. (link to plunker provided below) To tailor the code for your specific requirements, additional information would be necessary

<program-description>
<div class="__programDescription">
    <div class="__contents" ng-class="{less: hidden}">
        <div data-ng-bind-html="program.Description"></div>
    </div>
</div>

</program-description>


angular.module('myApp').directive('programDescription', function(){

    return {

         restrict: 'A',
         transclude: true, 
         template: '<div><div ng-transclude></div> <a ng-show="showToggle">{{ toggleText }}</a></div>',
         link: function( scope, element, attrs ){
               var limit = attrs.limit  ? parseInt(attrs.limit,10) : 200; // default to 200px
               scope.$watch(function(){
                   return element.find('.__programDescription').height();
               }, function(if (newValue > limit){
                   scope.showToggle = true;
               });

               .. additional code to manage more/less clicks.. 

         }
     }

})

a functional plunker example can be accessed at: http://plnkr.co/edit/Sm393HAzTRp8wqEPNkdg?p=preview

Answer №2

One way to determine whether you should display something is by examining the extent of your explanation.

Meaning and Application

The length property provides the number of characters in a string.

An empty string has a length of 0.

For instance:

<a href="#" ng-show="message.Content.length < 1000">Show More...</a>

Modification:

Incorporating @Andrew Tomlinson's suggestion, you can condense the logic into one sentence like so:

<a href="#"> (message.Content.length < 1000) ? 'Show More...' : 'Show Less...'</a>

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

Tips for incorporating a title and script into a Vue template using Nuxt.js

Looking to enhance your website's SEO by adding title, description, and other HTML meta tags? Wondering how to achieve this in a Vue template? For instance, let's say you want to include the meta tags in your template.vue file. Additionally, y ...

Imitating Long Polling technique causing delays in rendering the entire webpage

Summary In my turn-based web app, each turn has a 60-second limit. If a user ends their turn before the 60 seconds are up, the next user automatically begins their turn. The mechanism behind this involves a PHP page where data generation is controlled by ...

What is causing the border-radius property to malfunction on a particular website when using Internet Explorer?

We create browser add-ons for popular browsers like Google Chrome, Firefox, Safari, and Internet Explorer. Our add-ons inject HTML elements into various websites such as mail.google.com and email14.secureserver.net. The HTML element we use has a border-ra ...

What is the process for adding a dot to the package.json file using the npm-pkg command?

I need help using the npm pkg set command to create a package.json file with the following structure: { ... "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-l ...

Is it possible to bind browser keyboard scrolling to a div without explicit instructions?

Here is a question that closely relates to this one: Enable div to scroll by keyboard without clicking I am aware that explicitly focusing on the element will enable the desired behavior. My inquiry is whether there is a way to achieve this implicitly. ...

Seeking help with executing JQuery Ajax functions within a foreach loop

Currently, I am engrossed in the study of programming and endeavoring to construct a website utilizing .Net Core. The predicament at hand pertains to my limited acquaintance with JavaScript while incorporating two JQuery/AJAX functions on my Index page - o ...

Transfer results of SQL query from PHP to JavaScript array

Every day, I manually update a JavaScript variable in the following way: <script> var data = [ ['Austria','IBM','8284927','UF93NV', '10'], ['Spain','Dell&ap ...

Struggling to get Angular Formly to properly inject into my module

Hello there! I am having trouble injecting formly and formlyBootstrap into my module. angular.module('myApp', ['formly', 'formlyBootstrap', function config(formlyConfig) { formlyConfig.setType([ { ...

Radio buttons failing to submit

$(".dist_radio").click(function(event) { event.preventDefault(); $(".dist_radio").removeClass('dist_on'); $(".dist_radio").children('input').attr('checked', false); $(this).addClass('dist_o ...

Utilizing JavaScript to update the content of a React page

Recently, I came across an API using Swagger for documentation. Swagger generates a ReactJs webpage to document and test the API endpoints. However, my lack of knowledge in React architecture has led to a problem: Prior to accessing any endpoint, an authe ...

Can we execute a function once a mandatory field in a form has been validated successfully?

Looking for a way to execute a function after validating required inputs in a form before submission. Any suggestions? And I'm specifically not using jQuery, but Angular ...

CSS - Implementing two stylesheets sequentially in an Opencart website

Currently, I am encountering a frustrating issue on my website. The CSS files are loading in a peculiar order - first the default styles, followed by my custom overrides in luke.css. This results in a brief display of cream color on the left sidebar before ...

Obtain file paths from a folder using JavaScript

Currently, I am in the process of developing an npm package that validates imported images. Can anyone provide some insights on how to instruct javascript to iterate through a specific directory and store the file paths in an array? Furthermore, my aim is ...

Implementing overlay content areas on a Bootstrap 3 website when the responsive menu is activated

Is it possible to create a semi-opaque overlay with 50% alpha on the content areas of my Bootstrap 3 website when the nav-stacked menu is opened, but only when viewed on a phone (@media (max-width: 767px))? Can this be achieved using CSS alone or will jQue ...

Generating a clickable link for the URL included in a tweet message

As someone who is just starting out in coding, I could really use some help. Currently, I am using node.js to fetch tweet text and displaying it using html. I would like to add a hyperlink for the URLs within the text. For example: #Times #News Ukrainians ...

Select state and city options similar to the national breakdown page

I'm attempting to replicate a state and city selection box similar to the one on the NTTS Breakdown website. You can see it in action here: When you select a state on the left side of the webpage, the city selection box displays "loading data" and th ...

Is your Facebook send button appearing strangely? Find out how to fix it here!

I recently integrated the Facebook send button on my website, allowing users to easily share information about each drive listed. However, a new issue has arisen where clicking on the send button opens a popup inside a table, negatively affecting the page& ...

Retrieve the designated element from an array of JSON data in SPLUNK

As a newcomer to the world of Splunk, I am facing a challenge with handling JSON data. Here is an example of the JSON data I am working with: "request": { "headers": [ { "name": "x-real-ip", "value": "10.31.68.186" ...

Is there a way to capture an error message in the JSON fetch response?

Here is the code snippet to consider: fetch('https://api.flickr.com/services/rest/?method=flickr.photos.search' + '&api_key=thiskeyshouldgivemeanerror&text=dog&format=json' + '&per_page=24&nojsoncallbac ...

Retrieving information from a JSON web service can easily be done using just JavaScript and jQuery

I recently downloaded a sample application from the following URL: . I am pleased to report that the part I have implemented is functioning flawlessly: <script src="scripts/jquery-1.3.2.debug.js" type="text/javascript"></script> <script src ...