Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window resize directive

I was trying to make a div resize when the window resizes, after looking around, it seems that using a directive was the best solution.

Template:

<div elHeightResize ng-view ng-style="{ height: windowHeight }"></div> 

Directive:

myApp.directive('elheightresize', ['$window', function($window) {     return {         link: function(scope, elem, attrs) {             scope.onResize = function() {                 var header = document.getElementsByTagName('header')[0];                 elem.windowHeight = $window.innerHeight - header.clientHeight;             }             scope.onResize();              angular.element($window).bind('resize', function() {                 scope.onResize();             })         }     } }]) 

While I can log elem.windowHeight in scope.onResize, it doesn't seem to apply it to ngStyle

Am I still overlooking something?

EDIT:

<div ng-view resize style="height: {{ windowHeight }}px">

This solution seems to work, still interested into why using ngStyle wasn't working.

like image 516
woutr_be Avatar asked Apr 13 '14 15:04

woutr_be


People also ask

How do I resize a window in HTML?

resizeTo() The Window. resizeTo() method dynamically resizes the window.

What happens when window is resized?

The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.

What is window Onresize?

The onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.

How do I capture a Windows resize event?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.


2 Answers

I think you forgot to fire digest cycle by calling scope.$apply(); at the end of scope.onResize method

Anyways, I used following directive (took from HERE) that works for me:

Try to open debug view and change view height: Demo Fiddle

app.directive('resize', function ($window) {     return function (scope, element, attr) {          var w = angular.element($window);         scope.$watch(function () {             return {                 'h': w.height(),                  'w': w.width()             };         }, function (newValue, oldValue) {             scope.windowHeight = newValue.h;             scope.windowWidth = newValue.w;              scope.resizeWithOffset = function (offsetH) {                  scope.$eval(attr.notifier);                  return {                      'height': (newValue.h - offsetH) + 'px'                     //,'width': (newValue.w - 100) + 'px'                  };             };          }, true);          w.bind('resize', function () {             scope.$apply();         });     } });  

And usage:

 <div  ng-style="resizeWithOffset(165)"         resize          notifier="notifyServiceOnChage(params)"    >     /** ... */  </div> 

Dummy controller method usage:

$scope.notifyServiceOnChage = function(){       console.log($scope.windowHeight);     }; 

[EDIT]

Here is demo without jQuery library by using innerHeight

Demo 3Fiddle

like image 173
Maxim Shoustin Avatar answered Oct 22 '22 18:10

Maxim Shoustin


Since we work with a directive, we can always do some DOM manipulation by changing the height of the element inside the directive.

Example:

var app=angular.module('App', []); app.directive('elheightresize', ['$window', function($window) {     return {         link: function(scope, elem, attrs) {             scope.onResize = function() {                 var header = document.getElementsByTagName('header')[0];                 elem.windowHeight = $window.innerHeight - header.clientHeight;                 $(elem).height(elem.windowHeight);             }             scope.onResize();              angular.element($window).bind('resize', function() {                 scope.onResize();             })         }     } }]) 

Live example: http://jsfiddle.net/choroshin/FJvyb/

like image 32
Alex Choroshin Avatar answered Oct 22 '22 16:10

Alex Choroshin