Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Last active November 13, 2017 21:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save melbourne2991/8822609 to your computer and use it in GitHub Desktop.
Save melbourne2991/8822609 to your computer and use it in GitHub Desktop.
Angular Bootstrap Breakpoint Detect
//Below is an example of a directive that let's other directives know when there has been a change in a breakpoint, as well as a
//windows resize, the event is triggered on the window resize and broadcast to child scopes (for this reason it's best to place
//the directive on body or a similar high level element), the arguments contain the new previousBreakpoint will likely return
//null if there is yet to be a change in breakpoint.
app.directive('viewFrame', [function($scope) {
return {
controller: function($scope, $element) {
var width = function() {
return $element.width();
}
var getBreakpoint = function() {
var windowWidth = window.innerWidth;
if(windowWidth < 768) {
return 'extra small';
} else if (windowWidth >= 768 && windowWidth < 992) {
return 'small';
} else if (windowWidth >= 992 && windowWidth < 1200) {
return 'medium';
} else if (windowWidth >= 1200) {
return 'large';
}
}
currentBreakpoint = getBreakpoint();
angular.element(window).bind('resize', function() {
var newBreakpoint = getBreakpoint();
var previousBreakpoint = null;
if (newBreakpoint != currentBreakpoint) {
previousBreakpoint = currentBreakpoint;
currentBreakpoint = newBreakpoint;
}
$scope.$broadcast('windowResize', currentBreakpoint, previousBreakpoint);
});
}
}
}]);
//Usage in another directive.
app.directive('sidebar', function() {
return {
link: function(scope, element) {
scope.$on('windowResize', function(event, currentBreakpoint, previousBreakpoint) {
console.log(newBreakpoint);
});
}
}
});
@porjo
Copy link

porjo commented Sep 4, 2014

Thanks for the tip.

I've forked this and made some improvements here:
https://gist.github.com/porjo/8638530c53472e2b9d0c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment