Skip to content

Instantly share code, notes, and snippets.

@adrianoxavier
Created December 16, 2013 15:10
Show Gist options
  • Select an option

  • Save adrianoxavier/7988559 to your computer and use it in GitHub Desktop.

Select an option

Save adrianoxavier/7988559 to your computer and use it in GitHub Desktop.
AngularJS resize directive
/*
Usage
<div resize="{'attr': 'height', '*': 0.375}"> ...
*/
'use strict';
Application.directive('resize', function ($window) {
return {
restrict: 'A',
scope: {
'resize' : '@'
},
link: function(scope, element, attrs) {
var params = eval('(' + scope.resize + ')');
var w = angular.element($window);
scope.getWindowDimensions = function () {
return { 'h': $window.innerHeight, 'w': $window.innerWidth };
};
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
var value = (params.attr == 'height') ? newValue.h : newValue.w;
if (params['-'] && !isNaN(params['-'])) {
value = value - params['-'];
}
if (params['+'] && !isNaN(params['+'])) {
value = value + params['+'];
}
if (params['*'] && !isNaN(params['*'])) {
value = parseInt(value * params['*']);
}
if (params['/'] && !isNaN(params['/'])) {
value = parseInt(value / params['/']);
}
if (params.attr == 'height' || params.attr == 'both') {
angular.element(element).height(value);
}
if (params.attr == 'width' || params.attr == 'both') {
angular.element(element).width(value);
}
}, true);
w.bind('resize', function () {
scope.$apply();
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment