[英]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.