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

Issues with the functionality of AngularJS router implementation

I have searched through various resources like Stack Overflow and other blogs, but unfortunately, I haven't been able to fix the routing issue in my AngularJS code. Although there are no error messages, the routing functionality doesn't seem to b ...

Store text in a table format in your local storage

I need help figuring out how to save product and price information to local storage when an "add to cart" button is pressed. Can someone provide guidance on how to do this? Here is the code I currently have: body> <!-- Header--> ...

Retrieve the formcontrolname property of a FormGroup within a FormArray

I am currently facing an issue with my code. In my FormGroup, I have a FormArray containing 3 FormControls. My goal is to iterate through the FormArray and display the values of each control in a form. However, I am unsure about what to use for the formCon ...

Filter data in AngularJS checkboxes based on true and false values

When I check a checkbox <input type="checkbox" ng-model="search.isWorking" />, my list is filtered to show only true values, which works fine. However, when I uncheck the box, I want the list to show all values (both true and false), but Angular on ...

Converting HTML into an object using $compile

I am currently working on calling an HTML template and want to bind the data with Angular. I successfully retrieve the data to bind and the HTML, but when I try to compile it, it returns all the HTML binded as an object. How can I convert it back to plain ...

What is the appropriate way to add left padding or left margin to the modal footer in Bootstrap 5.3

I have been working on creating a modal that resembles a chat box. I have completed most of the work, but I am facing an issue with the input area not being fixed. My goal is to have the input text displayed on the footer of the modal, but it seems like th ...

Having trouble accessing the menu in the dropdown div when clicking back?

I am working on creating a menu that drops down from the top of the page using JQuery. I have everything set up with the code below: <div id="menu"> <div id="menucontentwrapper"> <div id="menucontent"></div> </di ...

What could be causing this table to exceed its dimensions by 2 pixels?

Why is the height of this table 102px instead of 100px? Is there another CSS attribute that needs to be set besides: table { border-spacing:0; border-collapse:collapse; } For example, check out this example link. ...

What is the best way to access and interpret headers received from my API using angular?

I have a piece of code similar to this on my website domain.com: $http.post("http://api.domain.com/Controller/Method", JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } }) ...

What is the best way to assign two styles with the same value in a single line of code?

Can you suggest a simpler method for expressing left:15%;right:15%, such as using left,right:15% or something equivalent? Your assistance is greatly appreciated! ...

GWT integration for TinyMCE

I've been attempting to incorporate TinyMCE with GWT's RichTextBox, but so far I haven't had any luck. The issue seems to be that GWT turns the Rich text area into a #document, rather than a standard textarea in HTML. Does anyone know how to ...

After pressing the login button, my intention is to transition to a different page

I am relatively new to web development and working with angular. I have a login component that, upon hitting the LOGIN button, should redirect to another component on a different page. However, currently, when I click the button, it loads the data of the o ...

Using Jquery for a Second Timer

I have a jQuery function that looks like the one below. The result is displayed in a span, but when the page is reloaded, this span briefly disappears and then reappears. Is there a way to prevent this from happening? I need it to stay visible at all tim ...

Issues with Angular ng-model-options not working properly in Internet Explorer

Is there a way to submit a form on keypress/enter while using ng-model-options="{updateOn: 'submit'}" Here is the template: <form ng-model-options="{ updateOn: 'submit' }"> <input type="submit" value="submit" style="visib ...

Creating responsive divs on my webpage

Although I have browsed through various posts with similar questions, none of the solutions seem to work for me. I will make sure to include all relevant information related to my issue and explain why I am unable to resolve it on my own. Currently, I am ...

Combine all input values into one using jQuery

I have four text boxes and I want to append the value of each box to a certain text box with a comma on KeyUp function, in addition to the default value. The issue is that it keeps overwriting the previous value. I am looking for an output like this: 0 ...

The correct organization of angular files

Seeking advice on the optimal angular file structure for my upcoming project. It will be a single page application featuring a video feed on the main page, as well as specific post viewing pages. The goal is to provide users with login capabilities, conten ...

Incorporate the operating hours of a Business

If I specifically choose Sunday and Monday with working hours ranging from 08.00 to 20.00, the output should be 1&08:00&20:00,2&08:00&20:00. How can I achieve this using Vue.js? This is my current code: <script> submitBox = new Vue( ...

Creating input fields similar to the Mail app on OSX

Is there a way to create input fields similar to those in the Mail App on OSX? Take a look at this screenshot - I'm looking to group text as shown in the image above. Instead of having multiple individual input fields (e.g. like in the mail app for ...

Hybrid App Date Selection Tool: Modern Calendar Picker for Cordova and Phonegap

Perhaps my question is repetitive, but I am searching for a way to display a native calendar style datepicker in Cordova/Phonegap apps on both Android and iOS. Despite numerous attempts, I have not been successful in finding a solution. I did come across a ...