Created
January 28, 2014 14:42
-
-
Save chrisjordanme/8668864 to your computer and use it in GitHub Desktop.
Angular Directive for Window Resize Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('your-module-name', []) | |
.directive('onResize', ['$window', function ($window) { | |
return { | |
link: function (scope, el, attrs) { | |
var initialWidth = $window.innerWidth, | |
smallClass = attrs.resizeClass || 'yourDefault', | |
smallAttr = attrs.resizeAttr || 'yourDefault', | |
smallWidth = attrs.resizeWidth || 1024; | |
var setSmall = function () { | |
el.addClass(smallClass); | |
el.attr(smallAttr, smallAttr); | |
}; | |
var setLarge = function () { | |
el.removeClass(smallClass); | |
el.removeAttr(smallAttr); | |
}; | |
if (initialWidth < smallWidth) { | |
setSmall(); | |
} else { | |
setLarge(); | |
} | |
angular.element($window).on('resize', function () { | |
if ($window.innerWidth <= smallWidth) { | |
setSmall(); | |
} else { | |
setLarge(); | |
} | |
}); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
---- angular 2.0 window resize directive.
import { Directive, ElementRef} from 'angular2/core';
@directive({
selector: '[resize]',
host: { '(window:resize)': 'onResize()' } // Window resize listener
})
export class AutoResize {
}