简体   繁体   中英

Angular2 click and ngIF

I want to use (click) function only when ngIF have value True .

<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>

So I need to add ngIF in <tr> tag as if {{ domain.validated }} value is True , then (click) function works, else it don't work or show message that variable have value False . How to do that?

Try using this:

<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>

Hope it will work for you .

The function will only be called if first part(!domain.validated) will return false.

您可以向goToDetail函数提供第二个参数Boolean,并仅在用户为真时重定向。

Try this:

<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>

You can use ng-template also.

You can define your function like this:

gotoDetail(id: number){
    if (this.domain.validated)
    // Your normal code
    else 
    // Display error message, Do nothing
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM