Condition based click event in Angular 2

Angular

Angular Problem Overview


In my application I would like to have conditional based click event ,

<div class="trashIconDiv" (click)="if(idx > 0) {removeSelected(item.spId)}">

In the above code removeSelected function should be executed only when idx >0 , any idea how to implement

Angular Solutions


Solution 1 - Angular

(click)="idx > 0 && removeSelected(item.spId)"

Solution 2 - Angular

Simply use a ternary:

<div class="trashIconDiv" (click)="idx > 0 ? removeSelected(item.spId) : false">

This will only call removeSelected function when the condition is true, if it's false then it won't do anything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrefactorView Question on Stackoverflow
Solution 1 - AngularyurzuiView Answer on Stackoverflow
Solution 2 - AngularChrillewoodzView Answer on Stackoverflow