Skip to content

Instantly share code, notes, and snippets.

@perminder-klair
Created May 1, 2014 11:41
Show Gist options
  • Save perminder-klair/11449792 to your computer and use it in GitHub Desktop.
Save perminder-klair/11449792 to your computer and use it in GitHub Desktop.
Resize container as Window size (accepts percentage) for AngularJs
'use strict';
/**
* Usage: ng-style="style()" resize heightpercetange="50" widthpercetange="50"
*/
angular.module('app')
.directive('resize', function ($window) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var w = angular.element($window);
scope.getWindowDimensions = function () {
return { 'h': w.height(), 'w': w.width() };
};
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
var newHeight = newValue.h;
if (typeof attrs.heightpercetange !== "undefined") {
newHeight = (attrs.heightpercetange / 100) * newValue.h;
}
elem.css('height', newHeight + 'px');
var newWidth = newValue.w;
if (typeof attrs.widthpercetange !== "undefined") {
newWidth = (attrs.widthpercetange / 100) * newValue.h;
}
elem.css('width', newWidth + 'px');
}, 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