[英]Angular2 click and ngIF
我想在ngIF
值为True
时使用(click)
功能。
<tbody class="table-hover domains">
<tr *ngFor="let domain of domains" (click)="gotoDetail(domain.id)">
<td class="col-md-3">{{ domain.name }}</td>
<td>{{ domain.validated }}</td>
</tr>
</tbody>
所以我需要在<tr>
标签中添加ngIF
,就像{{ domain.validated }}
值为True
,然后(click)
函数有效,否则它不起作用或显示变量值为False
消息。 怎么做?
试试这个:
<tbody class="table-hover domains">
<tr *ngFor="let domain of domains" (click)="!domain.validated || gotoDetail(domain.id)">
<td class="col-md-3">{{ domain.name }}</td>
<td>{{ domain.validated }}</td>
</tr>
</tbody>
希望它能为你效劳。
仅当第一部分(!domain.validated)将返回false时,才会调用该函数。
您可以向goToDetail函数提供第二个参数Boolean,并仅在用户为真时重定向。
尝试这个:
<tbody class="table-hover domains">
<template ngFor let-domain [ngForOf]="domains ">
<tr *ngIf="domain.validated" (click)="gotoDetail(domain.id)">
<td class="col-md-3">{{ domain.name }}</td>
<td>{{ domain.validated }}</td>
</tr>
</template>
</tbody>
您也可以使用ng-template
。
您可以像这样定义您的函数:
gotoDetail(id: number){
if (this.domain.validated)
// Your normal code
else
// Display error message, Do nothing
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.