: I'm pretty new to Angular, so this could contains incorrect methodology. Feel free to give feedback if so.
angular.module('app.commonDirectives').directive(
'centerAlign',
function () {
return {
restrict: 'A',
controller: ['$scope', '$window', function($scope, $window){
$scope.innerHeight = $window.innerHeight;
angular.element($window).bind('resize', function () {
$scope.recalculateAlignment();
});
$scope.getWindowInnerHeight = function(){
return $window.innerHeight;
};
}],
link: function ($scope, $element, attr){
$scope.recalculateAlignment = function () {
var $modalContent = angular.element($element[0].querySelector('.modal-dialog')),
clientHeight = $modalContent[0].clientHeight + 60, // 30px margin on both sides taken into account
windowInnerHeight = $scope.getWindowInnerHeight(),
offsetHeight = windowInnerHeight - clientHeight;
$element.css('margin-top', String((offsetHeight / 2)) + 'px');
};
// add watch on ngShow attribute to trigger repositioning when modal becomes visible (which is required to retrieve height of modal content)
$scope.$watch(attr.ngShow, function() {
$scope.recalculateAlignment();
});
}
};
});