-2

simply I want to run a function on click only when certain condition exists , I don't need to add extra function in component ts file to check for the condition , I need to do it inline, like

 <button (click)="'condition==true'?runFunction()">
Arash
  • 3,458
  • 7
  • 32
  • 50

2 Answers2

1

You can do that by creating the function inside the component.ts

check(){
  if(condition){
     callThatFunction();
  }
}

and in HTML

<button (click)="check()">
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • it is a very simple condition, just checking for a flag, I don't want to make my component complicated just to check for a flag – Arash Oct 14 '17 at 12:29
  • haha! why do you think it makes your component complicated? its the same thing – Sajeetharan Oct 14 '17 at 12:30
0

As @Sajeetharan noted, its best to do the check inside your controller. If its not suitable for you, you could also have 2 buttons in your template, like this:

<button *ngIf="condition" (click)="runFunction()">
<button *ngIf="!condition">
Martin Adámek
  • 16,771
  • 5
  • 45
  • 64