如何在 angular 中的一个元素上应用多个模板绑定
How to apply multiple template bindings on one element in angular
我正在使用如下模板:
<ul [ngClass]="{dispN: !shwFilter,'list-group':true,'autoS':true,'dispB':shwFilter,'myshddw':true}" style=";display: none">
<li *ngIf="itsNotF && itsNotF.length" [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
<div *ngIf="valm1 && valm1.type=='1'">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<p style="margin: 8px;">{{valm1['body']}}</p>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
<div *ngIf="valm1 && valm1.type=='2'" (click)="modlTxt=valm1;notREadVu(j)" data-toggle="modal" data-target="#myModal">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
<div *ngIf="valm1 && valm1.type=='3'">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<p style="margin: 8px;">{{valm1['body']}}</p>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
</li>
<li [ngClass]="{bgDFF: !colps[j],'list-group-item':true,'lgOt':true}" (click)="logout()">
<span class="title">Log Out <i class="fa fa-sign-out"></i></span>
</li>
</ul>
所以它给出了以下错误:
EXCEPTION: Template parse errors:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * ("one">
<li *ngIf="itsNotF && itsNotF.length" [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" [ERROR ->]*ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
"): App@78:94
Previously it was not giving error I faced this issue after upgrading to RC4.
So what's workaround, so I can apply multiple template binding on single element without altering Template structure.
将您的 *ngIf 放在父级 div 中,*ngFor 可以保留在原处。
例如:
<div *ngIf="itsNotF && itsNotF.length">
<div [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
</div>
</div>
如果您正在使用 *ngFor 并想添加 *ngIf 来检查某些字段,如果条件不太复杂,我会使用过滤器,其中我 运行 我的条件和 return只有在该循环内进入我的条件的项目..希望它有帮助
类似的东西,我只想显示带有描述的项目:
<div class="delivery-disclaimer" *ngFor="let payment of cart.restaurant.payment_method | filter:[{short_name: cart.payment_method}] | onlyDescription" text-wrap>
<ion-icon item-left name="information-circle"></ion-icon> {{payment.pivot.description}}
</div>
达沃
您可以使用以下(扩展版本)来保留文档结构(例如,对于您的 css 选择器):
<template [ngIf]="itsNotF && itsNotF.length">
<div [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
</div>
</template>
不能在 Angular 2 中的一个元素上使用两个模板绑定(如 *ngIf 和 *ngFor)。但是您可以通过用 span 或任何其他元素包装元素来实现相同的目的。最好附加一个 <ng-container>
,因为它是一个逻辑容器,不会附加到 DOM。例如,
<ng-container *ngIf="condition">
<li *ngFor="let item of items">
{{item}}
</li>
</ng-container>
我正在使用如下模板:
<ul [ngClass]="{dispN: !shwFilter,'list-group':true,'autoS':true,'dispB':shwFilter,'myshddw':true}" style=";display: none">
<li *ngIf="itsNotF && itsNotF.length" [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
<div *ngIf="valm1 && valm1.type=='1'">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<p style="margin: 8px;">{{valm1['body']}}</p>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
<div *ngIf="valm1 && valm1.type=='2'" (click)="modlTxt=valm1;notREadVu(j)" data-toggle="modal" data-target="#myModal">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
<div *ngIf="valm1 && valm1.type=='3'">
<h5 style="padding:8px;margin: 0;">{{valm1['header']}}</h5>
<p style="margin: 8px;">{{valm1['body']}}</p>
<h6 style="padding:8px;margin: 0;">{{valm1['note']}}</h6>
</div>
</li>
<li [ngClass]="{bgDFF: !colps[j],'list-group-item':true,'lgOt':true}" (click)="logout()">
<span class="title">Log Out <i class="fa fa-sign-out"></i></span>
</li>
</ul>
所以它给出了以下错误:
EXCEPTION: Template parse errors:
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * ("one">
<li *ngIf="itsNotF && itsNotF.length" [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" [ERROR ->]*ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
"): App@78:94
Previously it was not giving error I faced this issue after upgrading to RC4.
So what's workaround, so I can apply multiple template binding on single element without altering Template structure.
将您的 *ngIf 放在父级 div 中,*ngFor 可以保留在原处。
例如:
<div *ngIf="itsNotF && itsNotF.length">
<div [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
</div>
</div>
如果您正在使用 *ngFor 并想添加 *ngIf 来检查某些字段,如果条件不太复杂,我会使用过滤器,其中我 运行 我的条件和 return只有在该循环内进入我的条件的项目..希望它有帮助
类似的东西,我只想显示带有描述的项目:
<div class="delivery-disclaimer" *ngFor="let payment of cart.restaurant.payment_method | filter:[{short_name: cart.payment_method}] | onlyDescription" text-wrap>
<ion-icon item-left name="information-circle"></ion-icon> {{payment.pivot.description}}
</div>
达沃
您可以使用以下(扩展版本)来保留文档结构(例如,对于您的 css 选择器):
<template [ngIf]="itsNotF && itsNotF.length">
<div [ngClass]="{bgDFF: !colps[j],'list-group-item':true}" *ngFor="let valm1 of itsNotF;let j=index;" (click)="togFltr(j)" style="padding: 0;background: #fff">
</div>
</template>
不能在 Angular 2 中的一个元素上使用两个模板绑定(如 *ngIf 和 *ngFor)。但是您可以通过用 span 或任何其他元素包装元素来实现相同的目的。最好附加一个 <ng-container>
,因为它是一个逻辑容器,不会附加到 DOM。例如,
<ng-container *ngIf="condition">
<li *ngFor="let item of items">
{{item}}
</li>
</ng-container>