Angular 和 ionic 2 : Showing 2 columns per row with NgFor and a conditional display

标签 angular ionic-framework ionic2

我想做的是在 ionic 2 应用程序中每行显示 2 列,当用户在搜索框中输入一些文本时,例如键入仅存在于第二行的文本,第一行一个消失了(现在确实发生了,但是第二列保留在右边,而不是把它放在隐藏的位置)。

这是我现在使用的代码,它没有让我的列在同一行中:

<ion-grid>
    <div [ngSwitch]="top">
      <div *ngSwitchCase="'resto'">
        <ion-row responsive-sm wrap *ngFor="let data of topRestaurants | ArrayObject ">
          <ion-col col-6 *ngIf="data.value['title'].toLowerCase().includes(textFilter)">
            <ion-card>
                <ion-col col-12 no-margin no-padding><img src="{{data.value['logo']}}"/></ion-col>
                <ion-col col-12 no-margin no-padding>
                  <ion-card-content>
                    <ion-card-title>
                      {{data.value['title']}} <ion-badge item-end>{{(data.value['distance']) | number:'1.2-2'}} km</ion-badge>
                    </ion-card-title>
                    <p>
                      <small><rating [score]="data.value['overall_rating']" [max]="5"></rating> ({{data.value['reviews_count']}}) Avis</small>
                    </p>
                  </ion-card-content>
                </ion-col>
            </ion-card>
          </ion-col>
        </ion-row>
      </div>

当我制作如下所示的 ion-col 样式时:display: inline-block, width:50%,它几乎可以正常工作,但即使 ngIf 为 false (因为该行仍然存在于 DOM 中,所以我们删除了 col :/)

enter image description here

最佳答案

您可以移动 *ngFor来自 <ion-row><ion-col> .你现在拥有它的方式可以防止 <ion-col>永远不会垂直移动,因为它们会保持在自己的行中。如果你移动 *ngFor你将只有一个<ion-row>与 n <ion-col>秒。当列宽为 6 ( col-6 ) 时,一行中只会有两列,因为当列宽超过 12 时,它将换行。当你的一件元素因为 *ngIf 而被移除时布局会自行调整。

移动 *ngFor 会出现一个问题-声明是你必须移动*ngIf ,因为在一个宿主元素 ( structural directive docs ) 上不能有多个结构指令 (*ngIf, *ngFor)。

关于 Angular 和 ionic 2 : Showing 2 columns per row with NgFor and a conditional display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485943/

相关文章:

angular - EventEmitter 在 Angular 2 中不起作用

angular - angular2中*ngFor中的*是什么意思?

javascript - JS/ ionic :TypeError: Cannot read property 'code' of undefined

android - 无法使用 Cordova 推送通知插件获取设备 token

javascript - 获取 NaN 而不是数字/值

php - 使用 Laravel 5.4 作为后端的 Ionic 2 登录

angular - 如何修复 "Could not determine the dependencies of task ' :app:preDebugBuild'"in Ionic 4 Cordova

ngFor 循环中点击的 div 的 Angular Return id

ionic2 - 推送页面加载时出现问题

javascript - 如何在 ionic 2 上循环对象数组?