Using AngularJS to apply custom css to a tag within a directive for creating a Bootstrap sticky footer

Currently, I am in the process of developing my very first AngularJS application with Bootstrap as the responsive framework. In order to achieve a sticky footer, I usually utilize jQuery to determine the outerHeight of the footer and then apply that value to the body tag as padding-bottom within the CSS.

However, I am attempting to avoid using jQuery in this project and have been experimenting with ways to accomplish this purely through AngularJS.

This is what the HTML looks like:

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name=viewport content="width=device-width,initial-scale=1, maximum-scale=1, user-scalable=0">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/style.min.css" rel="stylesheet" type="text/css" media="all" />
</head>

<body ng-app="ngApp">
    <header>
    </header>
    <main>
        <div class="container">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                        <!--  -->
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                        <!--  -->
                    </div>
                </div>
            </div>
        </div>
    </main>
    <footer footer-height>
        <div class="container">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                        <!--  -->
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                        <!--  -->
                    </div>
                </div>
            </div>
        </div>
    </footer>
    <!-- Scripts  -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap.min.js"></script>
    <!-- App -->
    <script src="app/app.js" type="text/javascript"></script>
</body>

For the JavaScript part:

var app = angular.module("ngApp", [])
    // footer height
    .directive('footerHeight', function() {
        return {
            link: function(scope, element) {
                scope.$watch(function() {
                    var footerHeight = element[0].offsetHeight;              
                    var elBody = angular.element(document).find('body');
                    elBody.css('padding-bottom', footerHeight);
                    console.log(footerHeight);
                });
            }
        };
    }); 

While the calculation of the footer's height is accurate according to the console logs, the issue lies in applying this value to the body tag.

Answer №1

If you want to customize the height of a div, consider creating your own directive and applying it to the element.

app.directive('customHeight', function(){
  return{
    restrict:'A',
    link: function(scope, element){
       return angular.element(element).prop('offsetHeight');
    }
  };
});

<div custom-height></div>

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 resolving a flickering issue that occurs when switching to an input field with our default value / placeholder plugin

In the realm of web development, numerous plugins cater to the implementation of HTML5's placeholder attribute in older browsers. The plugin we have opted for can be found here. Unlike some other alternatives, this particular plugin, with a few modif ...

"Troubleshooting a CSS Bug on a PHP Dynamic Web

I have a PHP web page that is dynamic. localhost/ctd/index.php Everything is working fine, except when I add a forward slash like this: localhost/ctd/index.php/ The CSS does not load. Is this a CSS bug or PHP issue? Here is the full code of index.php: ...

Access an image URL that is saved in a MySQL database

Currently, I am facing a challenge attempting to populate an img tag src attribute with a filename sourced from a mysql database. As of now, I retrieve two images through a single MySQL query (selected randomly). However, in the future, I anticipate needi ...

Tips for customizing text field appearance in Safari and Chrome

I haven't had a chance to test it on IE yet. My goal is to create a unique style for the background image and text box shape, along with borders, for the search bar on my website, [dead site]. If you check it out on Firefox or Opera and see the sear ...

Tips for customizing the appearance of the day button in a React Material-UI date picker

In my React project, I am using material-ui date picker and I am looking for a way to customize the styling of the day buttons. Specifically, I want to change the text color of the available days. By default, as seen in the screenshot, the text color is bl ...

Using highlights in an external template does not function as expected

I am new to using AngularJS and I have encountered a problem. I am currently incorporating Prism.js or Highlight.js into my website (same result). It works perfectly in index.html, but it doesn't seem to work in other templates that are loaded with n ...

Guide to placing a basic div directly over a basic html table td

I need to create a simple html table with labels in one column and content in another. What I want to achieve is placing a div over the content td while keeping the label visible. I am looking for the most efficient way to do this, possibly making the seco ...

I'm having trouble centering the links in my navigation bar using flexbox, and I'm not sure how to fix it

Struggling with creating a navigation bar for my portfolio-building project. The "Who needs a quote" element is correctly displaying on the left, but I can't figure out how to center align the links "Sports, business, politics". Tried using flexbox op ...

Unusual sidebar scrolling issues experienced in Firefox

Currently, I am in the process of developing an app with a scrolling sidebar. However, there seems to be some unexpected behavior when using Firefox: the content within the sidebar disappears partially below the top of the div if the mouse is positioned lo ...

D3.js: Unveiling the Extraordinary Tales

I'm currently working on a project that requires me to develop a unique legend featuring two text values. While I have successfully created a legend and other components, I am facing difficulties in achieving the desired design. Specifically, the cur ...

Ensure the menu bar remains at the top of the page without using the position:

Check out this awesome fiddle here! My menu bar looks great at the top, but for some reason it won't stay in place when the user scrolls down. I've tried adding position:fixed to the CSS under the first #cssmenu, but it messes everything up. Her ...

What methods are available for identifying non-operational pointer-events?

According to this resource, Opera 12 does not support pointer-events, causing issues with my website. Interestingly, they do support the property in CSS but don't seem to implement it correctly. Modernizr's feature detection doesn't help in ...

Unable to trigger JQuery .blur event

I am currently working on implementing server-side validation for an input field that needs to be validated when it loses focus. However, I'm running into an issue where the alert is not triggered when the input field loses focus. Here's a snipp ...

Do you have to host a node server to run Angular applications?

(say) I am currently working on a project that utilizes Laravel or PHP for the back-end and Angular for the front-end. In my setup, I am using angular.js files from a CDN, which should work fine. However, I find myself confused when tutorials and books me ...

What is the best way to implement a hover effect on multiple rows within an HTML table using Angular?

I am currently working on developing a table preview feature to display events. I previously sought assistance here regarding positioning elements within the table and successfully resolved that issue. Applying the same principles, I am now attempting to c ...

How can I achieve a smooth opacity transition without using jQuery?

Although I understand that JQuery offers functions for this purpose, I am interested in finding a solution using only pure javascript. Is there a way to change the CSS opacity setting over time? Perhaps utilizing a unix time stamp with millisecond precisi ...

What is the best way to create an oval-shaped background for a div element?

Can someone help me achieve an oval shape for my index page div? I tried using border-radius but the edges are too round for my liking. I am looking for a more specific result See the CSS code below for reference <div class="home-page"></div> ...

Place an item in front of a specific object within an array

Hey there, I am currently working on an Angular application where I encounter a scenario where I retrieve an object by clicking on it, and this object is part of an array. For example: New Object: {object3} Array of Objects: [{object1}, {object2}, ....] ...

Is it possible for me to create a list in alphabetical order beginning with the letter B instead of A

My task involves creating a summary of locations using the Google Maps API. The map displays letters corresponding to different waypoints, and I have a separate <div> containing all the route information. Currently, I have the route information stru ...

numerous requirements for formatting utilizing Css modules

Can someone help me figure out how to set multiple conditions using a ternary operator to style an element with Css modules? I'm struggling to find the right syntax. Is it even possible? import style from './styles.module.sass' const Slider ...