我想创建包含 3 列的响应式画廊网格。我正在使用以下代码迭代图像数组:
<ion-grid>
<ion-row *ngFor="let image of images; let i = index;" *ngIf="{{i % 3 === 0}}">
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i < images.length}}">
<img [src]="images[i].url" />
</ion-col>
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i+1 < images.length}}">
<img [src]="images[i+1].url" />
</ion-col>
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i+2 < images.length}}">
<img [src]="images[i+2].url" />
</ion-col>
</ion-row>
</ion-grid>
但它显示的错误如下:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * ("
<ion-grid>
<ion-row *ngFor="let image of images; let i = index;" [ERROR ->]*ngIf="{{i % 3 === 0}}">
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i < images.le"): ng:///AppModule/PhotoGalleryPage.html@16:62
TypeError: Cannot read property 'toUpperCase' of undefined ("
<ion-grid>
<ion-row *ngFor="let image of images; let i = index;" [ERROR ->]*ngIf="{{i % 3 === 0}}">
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i < images.le"): ng:///AppModule/PhotoGalleryPage.html@16:62
Can't bind to '*ngIf' since it isn't a known property of 'ion-row'.
1. If 'ion-row' is an Angular component and it has '*ngIf' input, then verify that it is part of this module.
2. If 'ion-row' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
<ion-grid>
<ion-row *ngFor="let image of images; let i = index;" [ERROR ->]*ngIf="{{i % 3 === 0}}">
<ion-col col-4 (click)="openPhoto(image)" *ngIf="{{i < images.le"): ng:///AppModule/PhotoGalleryPage.html@16:62
Parser Error: Unexpected token {, expected identifier, keyword, or string at column 2 in [{{i % 3 === 0}}] in ng:///AppModule/PhotoGalleryPage.html@16:62 ("ow *ngFor="let image of images; let i = index;" *ngIf="{{i % 3 === 0}}">
<ion-col col-4 [ERROR ->](click)="openPhoto(image)" *ngIf="{{i < images.length}}">
<img [src]="images[i].url" "): ng:///AppModule/PhotoGalleryPage.html@17:27
Parser Error: Missing expected : at column 14 in [{{i % 3 === 0}}] in ng:///AppModule/PhotoGalleryPage.html@16:62 ("ow *ngFor="let image of images; let i = index;" *ngIf="{{i % 3 === 0}}">
<ion-col col-4 [ERROR ->](click)="openPhoto(image)" *ngIf="{{i < images.length}}">
<img [src]="images[i].url" "): ng:///AppModule/PhotoGalleryPage.html@17:27
Parser Error: Unexpected token } at column 14 in [{{i % 3 === 0}}] in ng:///AppModule/PhotoGalleryPage.html@16:62 ("ow *ngFor="let image of images; let i = index;" *ngIf="{{i % 3 === 0}}">
我无法理解错误,因为我是 Ionic2 的新手。谁能帮我解决这个问题?
我的第一个错误是将这些大括号放在*ngIf
状态,当我删除大括号并重新运行项目时,它给出了如下错误:
一个元素上不能有多个模板绑定。仅使用一个 名为"模板"或前缀为 *
的属性
因此,在寻找解决方案之后,我遇到了这个SO答案:https://stackoverflow.com/a/40860957/1739882
因此,我的解决方案是:
<ion-grid>
<div *ngFor="let image of images; let i = index;">
<ion-row *ngIf="i % 3 === 0">
<ion-col col-4 (click)="openPhoto(image)" *ngIf="i < images.length">
<img [src]="images[i].url" />
</ion-col>
<ion-col col-4 (click)="openPhoto(image)" *ngIf="i+1 < images.length">
<img [src]="images[i+1].url" />
</ion-col>
<ion-col col-4 (click)="openPhoto(image)" *ngIf="i+2 < images.length">
<img [src]="images[i+2].url" />
</ion-col>
</ion-row>
</div>
</ion-grid>
通用 ionic2 图像网格。您可以配置 n 列数。Ionic2 多列图像网格