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.