最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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! :)

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! :)

Share Improve this question edited Jan 31, 2017 at 3:37 Josh Crozier 241k56 gold badges400 silver badges313 bronze badges asked Jan 30, 2017 at 19:11 Kalpesh MangeKalpesh Mange 631 gold badge2 silver badges6 bronze badges 3
  • Never use () or [] together with {{}}. Either one or the other but not together. (() and [] can be used together). – Günter Zöchbauer Commented Jan 30, 2017 at 19:19
  • @GünterZöchbauer Thank you for that pointer! () and [] together always remind me of the sweet [(ngModel)]. – Kalpesh Mange Commented Jan 30, 2017 at 19:22
  • Yup, called banana-in-a box, in case you want to know :D - to not mix it up with box-in-a-banana which is invalid ;-) – Günter Zöchbauer Commented Jan 30, 2017 at 19:23
Add a ment  | 

2 Answers 2

Reset to default 12

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 ment above, you never need to use event/property binding, (i.e., ()/[]/[()]), together with variable interpolation, (i.e., double-curly braces, {{}}).

Simply by removing the interpolation and calling the alert(artist.artist_id)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论