简体   繁体   English

角度:根据屏幕分辨率切换HTML

[英]Angular: switch HTML depending on screen resolution

How to change HTML layout depending on the screen resolution. 如何根据屏幕分辨率更改HTML布局。

Situation: I have a table with 10 columns, but if the browser width is smaller than 900px it looks ugly. 情况:我有一个包含10列的表,但是如果浏览器宽度小于900px,它看起来很丑。 I would like to change the HTML layout ( merge 3 columns to one ) when the size is or less than 900px. 当大小小于或等于900px时,我想更改HTML布局( 将3列合并为1列 )。

Use Bootstrap responsive grid classes. 使用Bootstrap响应网格类。 It will automatically handle responsiveness. 它将自动处理响应性。 If you want to use custom grid then you can use angular ng-class attribute to achieve responsiveness. 如果要使用自定义网格,则可以使用angular ng-class属性来实现响应。

By using media queries, you could hide some of the table columns: 通过使用媒体查询,您可以隐藏一些表列:

HTML: HTML:

<table>
    <tr>
        <td>Col1</td>
        <td class="show-under-900px">Col2, Col3</td>
        <td class="hide-under-900px">Col2</td>
        <td class="hide-under-900px">Col3</td>
    </tr>
    <tr>
        <td>Col1</td>
        <td class="show-under-900px">Col2, Col3</td>
        <td class="hide-under-900px">Col2</td>
        <td class="hide-under-900px">Col3</td>
    </tr>
    <tr>
        <td>Col1</td>
        <td class="show-under-900px">Col2, Col3</td>
        <td class="hide-under-900px">Col2</td>
        <td class="hide-under-900px">Col3</td>
    </tr>
</table>

CSS: CSS:

@media screen and (min-width: 900px) {
    .show-under-900px {
        display: none;
    }
    .hide-under-900px {
        display: table-cell;
    }
}
@media screen and (max-width: 900px) {
    .show-under-900px {
        display: table-cell;
    }
    .hide-under-900px {
        display: none;
    }
}

I have solved this based on a controller that changes a value based on width, I dont' remember why using media queries wasn't an option but here it is: 我已经基于基于宽度更改值的控制器解决了这个问题,我不记得为什么不使用媒体查询是一种选择,但是这里是:

angular.module('app')
.controller('mobileController', function ($scope, $window) {

    var mobileWidth = 768;

    $scope.isMobile = function(){
        $scope.mobile = false;
        if(document.body.clientWidth <= mobileWidth){
            $scope.mobile = true;
        }
    };

    angular.element($window).bind('load resize', function(){
        $scope.$apply(function() {
            $scope.isMobile();
        });
    });
});

you can then use ng-show based on the $scope.mobile. 然后,您可以基于$ scope.mobile使用ng-show。 hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM