Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use expression for (click) event

Tags:

angular

In Angular2 I have multiple buttons and on click they set a value. Is it possible to use expression in the click event ie., instead of calling a function can we set the value directly?

<button (click)="setValue(1)">1</button> 
<button (click)="setValue(1)">2</button>
<button (click)="setValue(1)">3</button>

to some thing like

<button (click)="{{value=1}}">1</button> 
<button (click)="{{value=1}}">2</button>
<button (click)="{{value=1}}">3</button>
like image 579
Mujibur Rahman Avatar asked Sep 19 '25 13:09

Mujibur Rahman


1 Answers

You can use expressions but don't use {{}}

<button (click)="value=1">1</button> 

Angular evaluates the value part when the attribute name is wrapped in [], () (or both [()] or when the value contains {{}}. Don't use [] and () together with {{}}.

{{}} stringifies the result. If you want to bind non-string values to properties don't use {{}} at all.

like image 67
Günter Zöchbauer Avatar answered Sep 21 '25 03:09

Günter Zöchbauer