【问题标题】:Ionic responsive grid doesn't work离子响应网格不起作用
【发布时间】:2015-08-19 08:06:24
【问题描述】:

我在 Ionic 中创建了一个无法正常工作的响应式网格。网格没有响应。因此,如果我添加许多按钮或从系统中删除这些按钮,它不会在不同的屏幕尺寸上自动调整并且不会设置换行符。它们要么相互合并,要么所有按钮都在小屏幕上彼此下方的列中。我该如何解决这个问题?

我添加或删除 JSON 文件中的所有按钮。从那里我将按钮解析到系统中。

JSON: 7 个按钮

[
  {
    "_comment": "Games",
    "type": "button",
    "id": "entertainmentButton",
    "icon": "ion-film-marker",
    "name": "Game and entertainment",
    "topage": "servicePage.html",
    "color": "white",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "backgroundcolor": "#0066FF",
    "font-size": "26px"
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  },
  {
    "_comment": "Logo",
    "type": "button",
    "id": "MainPageLogo",
    "icon": "",
    "image": "../img/icon/logo_moenchsweiler.png",
    "name": "second link",
    "topage": "",
    "function": "OpenLink()",
    "controller": "OpenLinkCtrl",
    "color": "",
    "backgroundcolor": "",
    "font-size": ""
  }
]

JavaScript:

var myApp = angular.module('starter', []);
myApp.config(['$sceDelegateProvider', function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist([
        'self',
        ''
    ]);
}]);

myApp.controller('generateHTMLCtrl', function ($scope, $http, $compile, $interpolate, $templateCache) {    
    $http.get('myjsonfile.json').success(function (data) {      
        for(var i in data){            
            var interpolated = $interpolate($templateCache.get("tpl").trim())(data[i]);            
            angular.element(document.querySelector("#loadhere")).append($compile(interpolated)($scope));            
        }               
    });
});

myApp.controller("OpenLinkCtrl", function ($scope) {    
        $scope.OpenLink = function () {
           alert("Link open");
        }    
});

HTML:

    <body ng-app="starter" class="padding" style="text-align: center">
   <div class="row responsive-md" ng-controller="generateHTMLCtrl" id="loadhere"></div>
   <script type="text/ng-template" id="tpl">
      <div class="col">
         <a style="color:{{color}}; background-color:{{backgroundcolor}} " id="{{id}}" class="{{type}}" href="{{topage}}" ng-controller="{{controller}}" ng-click="{{function}}"> <i class="{{icon}}"><br></i>{{name}}</a>
      </div>
   </script>
</body>

我需要在 HTMl 代码中修改哪些 Grid 是响应式的?

编辑:

它应该看起来像这里,例如:

<div class="row responsive-sm">
            <div class="col">
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
                <button style="width: 100px; height: 100px">Test</button>
            </div>
        </div>

此代码显示按钮如下:

在大屏幕上:

小一点:

非常小:

已编辑:

解决办法:

<div ng-controller="generateHTMLCtrl" id="loadhere">
            <script type="text/ng-template" id="tpl">
                <a style="color:{{color}}; background-color:{{backgroundcolor}};"
                   id="{{id}}" class="{{type}}" href="{{topage}}"
                   ng-controller="{{controller}}" ng-click="{{function}}">
                    <i class="{{icon}}"><br></i>{{name}}
                </a>
            </script>
        </div>

【问题讨论】:

    标签: angularjs responsive-design ionic-framework ionic


    【解决方案1】:

    你想要的是更像这样的东西:

    <div class="row" ng-repeat="image in images" ng-if="$index % 4 === 0">
            <div class="col col-25" ng-if="$index < images.length">
                <img ng-src="{{images[$index].src}}" width="100%" />
            </div>
            <div class="col col-25" ng-if="$index + 1 < images.length">
                <img ng-src="{{images[$index + 1].src}}" width="100%" />
            </div>
            <div class="col col-25" ng-if="$index + 2 < images.length">
                <img ng-src="{{images[$index + 2].src}}" width="100%" />
            </div>
            <div class="col col-25" ng-if="$index + 3 < images.length">
                <img ng-src="{{images[$index + 3].src}}" width="100%" />
            </div>
        </div>
    

    这将在每行创建最多 4 个图像,这些图像可以环绕和堆叠。您还可以通过更改 for 行中的 ng-if 语句,然后更改该行中的图像数量来增加或减少图像数量

    当您在第四张图片上时,您将显示行类,从而创建一个新行 您将为您的行预先分配四列,但您将首先检查它是否会被填充以防止未定义的异常 如果图像存在于指定索引处,则将其添加到列中

    编辑! 试试这个 添加这个css类:

    .gallery {
        -webkit-flex-wrap: wrap;
        flex-wrap: wrap;
    }
    

    然后把它放在你的 html 中:

    <ion-content ng-controller="ExampleController" ng-init="loadImages()" class="gallery">
      <span ng-repeat="image in images">
        <img src="{{image}}" width="150px">
      </span>
    

    因为 ionic 是建立在 flex box 之上的,所以你可以在 ionic 中创建一个 flexbox 网格。

    【讨论】:

    • 你能给我一个具体的例子吗?你可以更正我的代码吗? (上面可以看到我的 HTML 示例)
    • 这个例子可能更容易理解:blog.nraboy.com/2015/03/…
    • 我已经知道这个例子了。不幸的是,我没有继续这样做。
    • 查看我的编辑,这是另一种方法的替代方法。
    • 感谢您的指导。解决方案是使用不带row responsive-smcol 类的div。而不是row responsive-smcol,您也可以使用您的建议.gallery。我已经修改了我的问题并添加了解决方案。
    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2019-10-18
    • 2018-10-07
    • 2016-02-16
    相关资源
    最近更新 更多