1

I have a sidebar which I collapse based on a value in $scope (let's say $scope.open for easy understanding). At the moment, when you click on the close/open button it just toggles that value, but if the menu is open it's going to look really ugly on mobile devices or small windows. So, I want to close the menu (set $scope.open to false) if the user is on a mobile sized device or using a small screen. Normally in CSS I would just use @media... and create a media query to add some styles on mobile devices, but in this case I want to do something similar but with a $scope variable.

Basically I want to do the equivalent of a media query but with my $scope.open variable to make sure the menu minimizes when on mobile. Any ideas?

germainelol
  • 3,231
  • 15
  • 46
  • 82

4 Answers4

1

maybe you can do something like

$scope.open = $(document).height() > 500; // can be different
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

May be a good idea create a directive to access dom using jQuery:

.directive('onloadDirective', function(){
return {
   restrict: 'A',
   scope: {
      open: '='
   }
   link: function(scope, elem, attrs){
       $(window).load(function() {
           //get element
           var myElement = $("#myElement");
           if (myElement.width() > 100 && myElement.height() < 200) {
               scope.open = true;
           } else {
               scope.open = false;
           }
       });
   }

} });

Solution with vanilla js and angular

    .directive('onloadDirective', function(){
return {
   restrict: 'A',
   scope: {
      open: '='
   }
   link: function(scope, elem, attrs){
       window.onload = function() {
           //get width
           var w = window.innerWidth;
           if (w > 100 && w < 200) {
               scope.open = true;
           } else {
               scope.open = false;
           }
       });
   }

} });

and your HTML can be like this:

<body ng-controller="myController" onload-directive open="open">
</body>
Luiz Mitidiero
  • 1,130
  • 1
  • 10
  • 22
  • I like the logic of the directive, but I'd reaaally want to use native JS or a pure angular solution. I really don't want to use jQuery to solve this just because it's easier :P – germainelol Nov 18 '15 at 17:26
  • haha no problem. We can remove jquery and use vanillajs on the directive. 1 min. – Luiz Mitidiero Nov 18 '15 at 18:26
0

If you are using bootstrap, Then the right way would be to add hidden-xs and hidden-sm class to your sidebar div.

This will hide the sidebar on mobile devices, While it will still be visible on all the devices having screen resolution ≥992px. So it doesn't look bad on them.

Vivek
  • 11,938
  • 19
  • 92
  • 127
0

Building on Luiz's answer, you can create a directive

.directive('onloadDirective', function($window){
return {
   restrict: 'A',
   scope: {
      open: '='
   }
   link: function(scope, elem, attrs){
      if ($window.innerWidth < 200) {
          scope.open = true;
      }
   }
  }  
});

Add the directive to the body tag or your ui-view, bind to your scope variable (don't put primitive values on the scope - What are the nuances of scope prototypal / prototypical inheritance in AngularJS? )

Community
  • 1
  • 1
sirrocco
  • 7,975
  • 4
  • 59
  • 81