Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 click and ngIF

Tags:

angular

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?

like image 279
Trimidas Avatar asked Dec 13 '16 10:12

Trimidas


People also ask

Why * is used in ngIf?

The *ngIf directive is most commonly used to conditionally show an inline template, as seen in the following example. The default else template is blank.

Can we have two ngIf in angular?

We can use multiple conditions in *ngIf with logical operator AND (&&) to decide the trustworthy of *ngIf expression. If all conditions are true, then element will be added to the DOM.

What is ngIf?

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.

How do you call a ngIf function?

Try *ngIf="condition && yourfunction()" . Your function must return true to the if evaluate to true, but it will only be executed if your condition is true, since an and operator will stop on first false.


1 Answers

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.

like image 145
Viplock Avatar answered Oct 04 '22 20:10

Viplock