Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the view based on screen size

Tags:

angularjs

I am aware of a similar question being asked before:

Change the templateUrl of directive based on screen resolution AngularJS

This was first asked over a year ago and since then AngularJS got changed a bit. I am curious to find out if there are any other ways to achieve something similar as I haven't found many information about templateUrl swapping, so maybe I am barking up the wrong tree here.

I have a single page app without any routes.

html:

<body ng-app="App">
  // lots of html same for both desktop/mobile
  <my-dir></my-dir>
  // even more here
</body>

template 1:

<p>Mobile</p>

template 2:

<p>Desktop</p>

I would like to render template 1 when the screen goes below 700px and template 2 otherwise. The templates change just what is inside my-dir directive. For example Template 1 renders list and template 2 renders table.

Another requirement would be to make it responsive if possible(aka templates would change as you resize the window)

At the moment I can use the solution from the above questions but are there any other ways to do it?

like image 456
Kocur4d Avatar asked Oct 23 '15 13:10

Kocur4d


People also ask

How do I make my website resize automatically when screen resolution changes?

By using media query you can specify any size of any element in any display size like this: @media (min-width: 640px) { body {font-size:1rem;} } @media (min-width:960px) { body {font-size:1.2rem;} }

How do I manage screen size on android?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.


2 Answers

In your controller:

$scope.includeDesktopTemplate = false;
$scope.includeMobileTemplate = false; 
var screenWidth = $window.innerWidth;

if (screenWidth < 700){
    $scope.includeMobileTemplate = true;
}else{
    $scope.includeDesktopTemplate = true;
}

html template:

<body ng-app="App">
    <p ng-if="includeMobileTemplate">Mobile</p>
    <p ng-if="includeDesktopTemplate">Desktop</p>
</body>

Hope it helps

like image 168
Jahongir Rahmonov Avatar answered Sep 23 '22 17:09

Jahongir Rahmonov


You can add window resize and scroll event listener on my-dir directive:

angular.module("App").directive('myDir', ['$window', '$timeout', function($window, $timeout){

    return {
        restrict: 'EA',
        scope: {},
        template:'<div>
            <p ng-if="showFirstTemplate">Mobile</p>
            <p ng-if="showSecondTemplate">Desktop</p>
            </div>',
        link: function(scope, element, attr){

            function checkTemplateVisible(event){

                 //use $timeout to make sure $apply called in a time manner
                 $timeout(function(){

                      //pageYoffset is equal to window scroll top position
                      if($window.pageYOffset > 700){
                          scope.showFirstTemplate = true;
                          scope.showSecondTemplate = false;
                      }else{
                          scope.showFirstTemplate = false;
                          scope.showSecondTemplate = true;
                      }
                 })

            })

            //scroll event make sure checkTemplateVisible called on browser scrolling
            $window.on('scroll', checkTemplateVisible)

            //resize event make sure checkTemplateVisible called on browser resizing
            $window.on('resize', checkTemplateVisible)
        }
    }


}])
like image 40
MarkoCen Avatar answered Sep 22 '22 17:09

MarkoCen