JSFiddle - React, Tailwind, and code Playground

Window Resize Directive

by Andrew Pougher

HTML

<div ng-app="miniapp" class="box" ng-controller="AppController" ng-style="style()" resize>window.height: {{windowHeight}}
    <br />window.width: {{windowWidth}}
    <br />{{w}}x{{h}}
    
    <div style="width:{{w -23}}px;height:{{h-44}}px;" id="bx">
    
    </div>
</div>

CSS

div {
    border:1px solid red;
} #bx {
    border:3px solid blue;
}

JavaScript

var app = angular.module('miniapp', []);

app.controller('AppController', function($compile, $scope, $window) {
 angular.element($window).bind('resize', function () {
    $scope.w = document.querySelector(".box").offsetWidth;
      $scope.h =  document.querySelector(".box").offsetHeight;
});
})



app.directive('resize', function ($window) {
    return function (scope, element) {
        var w = angular.element($window);
        scope.getWindowDimensions = function () {
            return {
                'h': w.height(),
                'w': w.width()
            };
        };
        scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
            scope.windowHeight = newValue.h;
            scope.windowWidth = newValue.w;

            scope.style = function () {
                return {
                    'height': (newValue.h - 100) + 'px',
                        'width': (newValue.w - 100) + 'px'
                };
            };

        }, true);

        w.bind('resize', function () {
            scope.$apply();
        });
    }
})