Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass values as parameters to a function call inside onclick in Angular2 without ngClick?

Angular2: I am trying to pass values from an *ngFor loop as a parameter to function call on the (click) attribute (since calling functions on onclick is not allowed for security reasons) event on my anchor tag.

Template Code:

<div *ngFor='let artist of artists'>
   <a (click)="alert({{artist.artist_id}})" href="#">{{artist.artist_name}}</a>
</div>

Compile Time error:

Got interpolation ({{}}) where expression was expected in [alert({{artist.artist_id}})]

Problem:

How do I pass the value of artist to the function call on (click)?

Any/All help appreciated! :)

like image 266
Kalpesh Mange Avatar asked Jan 30 '17 19:01

Kalpesh Mange


People also ask

Can we pass parameter in onclick function?

Your onclick event handler can optionally accept a MouseEventArgs parameter which Blazor will automatically include when it invokes the handler.

How do you pass a function on onclick event?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How do you pass parameters to Onclick react?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

How do you pass dynamic values on onclick HTML?

For passing multiple parameters you can cast the string by concatenating it with the ASCII value like, for single quotes we can use &#39; var str= "&#39;"+ str+ "&#39;"; the same parameter you can pass to the onclick(str) event.It is helpful in cases where we pass multiple parameters.It works with every browser.


1 Answers

In your case, you don't need the double-curly braces since you are passing the variable to a function in a event binding click attribute, therefore you can remove those braces and it will work as expected:

<div *ngFor='let artist of artists'>
   <a (click)="alert(artist.artist_id)" href="#">{{artist.artist_name}}</a>
</div>

According to the relevant documentation, you only need to use interpolation when inserting a value between HTML element tags and within attribute assignments (not to be confused with event/property binding).

For instance, you would need to use interpolation for this direct src attribute assignment, otherwise the value would be interpreted as a string:

<img src="{{pathUrl}}" />

However, if you were to use property binding (i.e., [src] rather than src), it would be treated as a variable and the interpolation wouldn't be required:

<img [src]="pathUrl" />

The same would apply to event binding like in your case.

In other words, as stated by Günter in the comment above, you never need to use event/property binding, (i.e., ()/[]/[()]), together with variable interpolation, (i.e., double-curly braces, {{}}).

like image 73
Josh Crozier Avatar answered Oct 30 '22 19:10

Josh Crozier