Adjusting the height of an element based on changes in screen width or height using Angular

I'm in the process of developing a directive that can detect changes in screen size, either width or height, and adjust the height of my element accordingly.

The main goal is to have my element extend all the way to the bottom of the browser window.

This is the code for my directive:

.directive('strechToBottom', ['$document', '$window', function ($document, $window) {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {

            var space;

            scope.$watch(function () {
                return $window.innerHeight;
            }, function () {
                space = $window.innerHeight - element[0].offsetTop;
                element.css('min-height', space + "px");
            });

        }
    };
}]);

Initially, everything works fine when the element is first created. However, the issue arises when I resize the screen - the function isn't triggered immediately. It seems like there's a delay before the function kicks in during the cycle, which is confusing to me.

I also attempted to listen for innerWidth changes, but that approach didn't yield any results either.

What could be causing this problem in my code?

Answer №1

When you bind to all scope digests, as seen by passing a function to `$scope.$watch` as the first argument, the function will execute in a non-deterministic manner from your perspective. Essentially, it will only run upon some interaction with the application (though this is a simplification).

If you want to do something when the viewport is resized, utilize the native `resize` event:

angular.element($window).on('resize', function () {
    $scope.$apply(function () {
        // ... add your code here
    });
});

If the above method does not cover all instances of resizing that you need to monitor, you can continue with what you are currently doing and also trigger a `$digest()` on window resize:

angular.element($window).on('resize', function () {
    $scope.$digest();
});

However, keep in mind the potential performance consequences of binding functions to be executed during every digest cycle.

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

Fixed position toolbar within a container positioned beside a dynamically sized sidebar navigation

I'm trying to figure out how to create a fixed toolbar that fills the remaining width of the page when a side nav is collapsible. Setting the width to 100% causes it to overflow the page due to the dynamic nature of the side nav. Using calc() isn&apos ...

How can I disable the background color in Chrome autocomplete? Is there a way to make the background

There's a form on my website where the autocomplete feature shows a blue background color. The form itself has a semi-transparent white background. I managed to change the autofill color to white using this CSS property: -webkit-box-shadow: 0 ...

Achieving perfect alignment both horizontally and vertically using CSS3's viewport height of 100%

I'm trying to utilize CSS3's Viewport Height to create a fullscreen section with a height of 100vh. However, I am encountering difficulties in centering elements both horizontally and vertically within the section. My goal is to have an image and ...

“How can controllers from multiple modules be integrated into the markup using angular syntax?”

Here's this particular question on SO that suggests the possibility of utilizing controllers from different modules by defining specific tag attributes like: <div ng-controller="submodule1.controller1">{{ whatever1 }}</div> <div ng-con ...

Determining the Height of a Navigation Bar Automatically with CSS

My website is built using React and react-strap. I set up my .header-overlay to cover the entire background within the .header. However, due to the presence of the .navbar, the .header-overlay extends beyond the boundaries of the .header vertically. I tho ...

What is the process for creating a personalized module in AngularJS?

Looking to create a bespoke module for AngularJS, but struggling to locate comprehensive documentation. What is the best approach to develop a custom AngularJS module that can be easily shared with others? ...

Tips for turning off hash-based redirection in AngularJS

Here is a specific URL: http://www.something.com/sometest/test/#registration Some code has been written based on #registration for Internet Explorer. However, when using AngularJS, it redirects to the following URL, which disrupts the logic: http://www ...

When attempting to open a link in a new tab, the ng-click function fails to execute

In Angular, utilizing both the <code>ng-click and ng-href directives at the same time will result in the click function being executed first. In this scenario, clicking on a link that navigates to Google will be prevented and instead an alert will be ...

Discovering how to create a line break within a text area using the $scope feature in Angular

I'm looking to incorporate a text area for chat input that is resizable. I would like to have some pre-filled texts in it, such as: Hi Jhon, Thanks for contacting us.... I want the text to appear on a new line after the existing content in the textar ...

Tips for validating user input in AngularJS without using a form tag

Within a popup, I am displaying HTML content that has been copied from another div and shown in the popup. I need to validate this input field to ensure it is required, and display an error message below the input box. <!-- HTML code for changing zip c ...

Chrome is where all the action happens, while Firefox prefers to keep things on the left

After a long break, I returned to designing my website and re-learning CSS. My approach involved creating a WordPress-integrated site with minimal HTML/PHP work, focusing more on utilizing the available CSS classes and IDs provided by a template theme (Bla ...

Angular UI Router Uib Modal: Refresh the page below when clicking outside the modal

I am currently using angular-ui-router-uib-modal and have a query regarding it. After closing my modal (which confirms a successful/failed operation), I want to reload the page below that displays a list of data. In the example provided, I can define thi ...

When converting the .html file to a .php file, the CSS file fails to be linked correctly

The issue I'm facing seems to be specific to the index.html file. The CSS that functions properly with an html extension suddenly stops working when the extension is changed to php. Upon inspecting the reference link in both cases, I noticed that when ...

Delete the span element if the password requirements are satisfied

I am implementing password rules using span elements. I aim to dynamically remove each span that displays a rule once the input conditions are met. Currently, I have succeeded in removing the span related to the minimum length requirement but I am unsure h ...

"Exploring the world of AJAX-generated content and the art of

I have developed an AngularJS application with multiple views that function as separate pages, yet the app itself is designed as a single page application to avoid reloading. I have come across information about utilizing escaped fragment URLs to guide s ...

Tips on ensuring a <video> element occupies the full height and width of the screen

I am currently working on an Angular 6 project where I am live streaming a user's webcam to an HTML element. My goal is to simulate the experience of capturing a video or photo on a mobile device. However, the size of the video is currently small. I ...

Guide on jQuery: Concealing the scrollbar on the body

Is there a way to hide the scroll bar using Jquery? I tried this code for Chrome but need a solution that works for all browsers. $ ::-webkit-scrollbar { display: none; } Any suggestions on how to achieve this cross-browser compatibility? ...

What is the best way to link styleUrls to a CSS file located in a different directory?

I am trying to include the CSS file src/app/shared/layouts/admin/admin.component.css in my component located at src/app/admin/create-company/create-company.component.ts How can I properly reference this css file in the styleUrls of create-company.componen ...

Bootstrap 4 Alpha Grid does not support aligning two tables side by side

When working with Bootstrap 4, I am aware that the .col-xs classes have been removed. Instead of using .col, I have tested it on Bootstrap 3.5 which is currently being utilized. <div class="col-xs-4"> Table content for left ...

Is it possible to utilize Ionic for creating a hosted web application without the need for node.js?

My curiosity lies in utilizing Ionic for constructing a web application. The layout, form elements, and integration with angular.js all appear to be well-designed. However, I have noticed that Ionic is mainly tailored towards developing native apps for An ...