Is there a way to track and observe the vertical movement of an element within a directive's link function?

Can you help me figure out the best way to $watch or bind the height position of an element? I have a scenario where I display three divs, one at a time in different tabs (using bootstrap). Each div has a different height, and my directive comes after these divs. My objective is to display it only when the div's height is greater than the current viewport height. Is there a way to achieve this without relying on scrolling since the initial position of my directive may vary without any scrolling involved?

Below is the code snippet of my directive:

(function(angular) {
  var app = angular.module('pi.core');
  app.directive('piGoUp', [
    '$location',
    '$window',
    '$document',
    function($location, $window, $document) {
      return {
        restrict: 'AE',
        replace: true,
        transclude: true,
        scope: {
          classes: '@',
          image: '@',
          idElement: '@'
        },
        link: function(scope, element, attrs) {
          console.log('goUp', element);

          if (!scope.image) scope.image = 'go-up.svg';

          scope.height = $window.innerHeight;

          angular.element($window).bind('resize', function() {
            scope.height = $window.innerHeight;
            console.log(scope.height);
            console.log($window);
            scope.$digest();
          });
        },
        controller: function($scope, $location, $anchorScroll) {
          $scope.goTo = function(idelement) {
            $location.hash(idelement);
            $anchorScroll();
          };
        },
        template:
          '<div class="pi-go-up {{classes}}"><div class="pi-go-up-text" ng-transclude></div><div><img src="{{image}}" ng-click="goTo(idElement)"></div></div>'
      };
    }
  ]);
})(angular);

I am looking for a way to hide it based on the height of its parent div, a specific element's height, or any other suggestions you may have...

Thank you

Answer №1

Swap out bind for addEventListener:

link: function(scope, element) {
  $window.addEventListener('resize', function() {

    // Implement necessary actions here; compare parent size to viewport
    // or element size and so on;
  }, 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

The status of Twilio Device consistently shows as offline

I'm currently working on integrating the Twilio Client into an AngularJS application with the server side running on Laravel PHP. Following the Quickstart guide for PHP, I have successfully set up a capability token endpoint in Laravel to access in An ...

Assigning a class to a table row does not produce any changes

When a user clicks on a table, I am attempting to achieve two tasks using pure Javascript: Retrieve the row number (this functionality is working) Change the background color of the row Below is my current code snippet: document.querySelector('#t ...

Problem with the document.form in Javascript

I've encountered an issue while running the code and I'm having trouble figuring out what's causing it. The code was running fine before, but now I'm facing this problem: function makeUnderline(formName,editor) { var txt = window.p ...

angular.bootstrap: How can I detect when initialization has finished?

Check out the jsfiddle below for more information In a project I'm currently working on, I have a requirement for 2 separate "app" to coexist side by side. By using angular.bootstrap, I am able to manually initialize multiple "app". Here's how i ...

Is there a javascript function that performs the reverse action of indexof()?

Is there a JavaScript function that is the opposite of indexOf()? In my code, when you press the button it will display 3 [from indexOf(".")]. However, I am looking for a function that is the opposite of indexOf(), which would show me 2 [decimal]. http: ...

Open the HTML document and access the file contents

I'm having trouble reading a text file from my computer onto a website. It works fine in Internet Explorer, but won't work in Chrome. I don't have much experience with HTML, so any help would be much appreciated! <html> <body> ...

Is it possible to streamline multiple $.fn. calls using javascript or jQuery?

I recently started using datatables and was advised to include the following code: $.fn.dataTableExt.oStdClasses.sWrapper = 'no-margin last-child'; $.fn.dataTableExt.oStdClasses.sInfo = 'message no-margin'; $.fn.dataTableExt.oStdClasse ...

Is it possible to dynamically load JavaScript?

I'm currently working on a project that requires me to dynamically add JavaScript. I have a global.js file that contains all the global variables I need to add dynamically. However, I'm facing an issue where global.js is not being added before ut ...

Passing parameters with AngularJS can return as Resteasy @FormParam being null

Struggling to integrate AngularJS on the front end with Resteasy as my Rest API. The issue I'm encountering is that my @FormParam values are always null when sending parameters using AngularJS. Below is a snippet of my JavaScript code: $scope.addPe ...

Unable to utilize Bower due to a node.js malfunction

Currently facing an issue while attempting to utilize bower for installing all necessary components for my website project. Each time I make an attempt, the following error presents itself: TypeError: Object #<Object> has no method 'toLowerCase ...

Loading state with suggestions from autocomplete feature on React

In my current project, I have a component that consists of input fields and a button. The button is set to be disabled until the correct values are entered in the input fields. Everything works as expected, but there's an issue with Chrome auto-fillin ...

Ways to release a client-side script with npm?

Within my nodejs package, I have included code that can be executed on both the backend and in a single .js file for browsers. In order to utilize the browser script, it must be placed within a script element in an HTML file. My query pertains to whether t ...

Using the AJAX post method to generate a JSON object from database results and send it back to a jQuery UI Dialog

I wrote a script that loads sample images from the database based on the relevant category when the page loads. Here's a simplified version of the PHP script: <?php $category = 'granite'; $samples = 'SELECT * FROM material WHERE ma ...

Troubleshooting Asynchronous Code in JavaScript

I was experimenting with creating a brute force poll voting script. $('#vote').click(function(e) { var votes = 0; var attempts = 0; var failures = 0; for(var i = 0; i < 500; i++){ ...

I need to mass upload a collection of resumes stored in a zip file, then extract and display the content of each resume using a combination of HTML

I recently used a service to extract and retrieve the contents of a zip file. I am trying to read the content of the files and integrate them into the scope of my Angular project. Any suggestions would be greatly appreciated. Below is an outline of my func ...

JavaScript: Remove duplicates and count the number of unique properties in an array

Arrays have always been a challenging aspect for me, so I would really appreciate any assistance. ...

Is there a way to ensure that clicking a link_to only opens a partial once?

These are the files I am working with: _comment.haml %div.comment{ :id => "comment-#{comment.id}" } %hr - if current_user && current_user.id == comment.user_id || current_user && current_user.id == reel_user = link_to " ...

Having trouble installing sqlite3? Encounter an issue like this: "srcdatabase.cc(35): error C2248: 'Napi::Env::DefaultFini': cannot access private member declared in class 'Napi::Env'"?

Encountering issues while trying to install sqlite3 for a Strapi app I've attempted using yarn to install sqlite3 in various ways, but to no avail. Below is the error log: Error message: Issue with installing sqlite3 when creating a Strapi app (..& ...

Utilizing the super method within a sails.js controller

Is there a way to call the default parent method of a redefined standard method in a controller created in sails.js? module.exports = { create: function(req, res) { //test some parameters if (condition) { //call regul ...

Setting URL parameters in a POST request: A guide

Currently, the data in question is structured as JSON within this code snippet. However, I've received feedback indicating that it should actually be implemented as URL parameters. I'm currently facing some difficulties with modifying this to fit ...