AngularJS + jQuery - window resize

AngularJS + jQuery - window resize

by Ankit Patidar

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>
<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>
window.height: {{windowHeight}}
    <br />window.width: {{windowWidth}}
    <br />
</div>

CSS

div {
    border:1px solid red;
}

JavaScript

var app = angular.module('miniapp', []);
app.controller('AppController',  function($scope) {});
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 - 200) + 'px', 'width': (newValue.w - 200) + 'px'
                };
            };
        }, true);
        w.bind('resize', function(){
            scope.$apply();
        });
    }
})